如何将列表中的数据导出到服务器端的CSV文件中? [英] how to export the data in a list to a CSV file in the server side?

查看:158
本文介绍了如何将列表中的数据导出到服务器端的CSV文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户列表和非用户列表,我通过使用for循环并将其遍历html表和放置在jsp页面中.如何在服务器端将数据导出到CSV. 在客户端将数据导出到CSV很复杂. 我对J2EE的了解很少.因此,请帮我解决这个问题.如果您可以提出建议,我可以照做.

I have a list of Users and Non-users list which I am putting it in a jsp page by using a for loop and iterating thru html table and . How do I export the data to CSV at the server side. Exporting the data to CSV in the client side is complicated. I have very less knowledge wrt to J2EE. So please help me with this.If you can suggest me an approach, I can follow that..

推荐答案

我能够做到这一点.

要求:在jsp中单击一个按钮时,它应该导出为CSV

Req:On clicking a button in jsp it should export to 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.

GetUserDetails is my servlet.

Servlet辅助代码.

Servlet side code.

        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!!

这篇关于如何将列表中的数据导出到服务器端的CSV文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆