【问题标题】:how to export the data in a list to a CSV file in the server side?如何将列表中的数据导出到服务器端的 CSV 文件?
【发布时间】:2014-05-23 19:05:12
【问题描述】:

我有一个用户和非用户列表,我通过使用 for 循环并遍历 html 表和 .如何在服务器端将数据导出为 CSV。 在客户端将数据导出为 CSV 很复杂。 我对 J2EE 的了解非常少。所以请帮我解决这个问题。如果你能建议我一个方法,我可以按照这个..

【问题讨论】:

标签: jsp csv export-to-csv


【解决方案1】:

我能够做到这一点。

要求:点击 jsp 中的按钮时,它应该导出为 CSV

jsp代码

 <div>

  <input type="button" name="exporttocsv"  onclick="window.open('${pageContext.request.contextPath}/GetUserDetails?exporttocsv=yes','toolbar=no','location=no','status=no','menubar=no','scrollbars=yes','resizable=yes','width=10','height=100');" value="Export to CSV"/>
</div>

GetUserDetails 是我的 servlet。

Servlet 端代码。

        protected void doGet(HttpServletRequest request, HttpServletResponse response)                         throws ServletException, IOException {

    // TODO Auto-generated method stub

    System.out.println("here inside get method");

    String clickevent=request.getParameter("exporttocsv");


    if(clickevent!=null &&clickevent.equalsIgnoreCase("yes"))
    {

        System.out.println("IN here....... download csv file...");
       //Getting the list from a session and iterating through the list
        HttpSession session = request.getSession();
                List<User> nonusers =          (List<User>)session.getAttribute("nonUserList");

        response.setHeader("Content-type","application/csv");
        response.setHeader("Content-disposition","inline; filename=test.csv");
        PrintWriter out = response.getWriter(); 
        out.println("Device Name,Non-Users");
        for(User nonUser: nonusers)
        {
        out.println(nonUser.getDeviceName()+","+ nonUser.getCecId());
           }

        out.flush();  
        out.close();  


    }

Thanks for your help!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 2020-02-25
    • 2020-01-09
    • 1970-01-01
    • 2021-11-05
    • 2013-03-19
    • 1970-01-01
    相关资源
    最近更新 更多