繁体   English   中英

PrintWriter写入CSV-新行不起作用?

[英]PrintWriter to write to CSV - New line not working?

我已经尝试过在网上以及在此特定网站上可以找到的所有内容,但这些选项均无效。 我正在遍历对象列表并创建CSV文件。 我想打破每个对象条目,但到目前为止还没有运气。

这是我目前拥有的:

    String value = FacesContext.getCurrentInstance().
            getExternalContext().getRequestParameterMap().get("cityId");

    String seperator = System.getProperty("line.separator");

    try {

        PortletResponse response1 = (PortletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        HttpServletResponse resp = PortalUtil.getHttpServletResponse(response1);
        resp.setHeader("Content-Type", "text/csv");
        resp.setHeader("Content-disposition", "attachment;filename=myFile.csv");

       List<Person> people = ppApi.getPeopleByCityId(Long.parseLong(value));

       PrintWriter out = resp.getWriter();

       for(Person pp : people) {

           out.print(pp.getFirstName() + ",");
           out.print(pp.getLastName() + ",");
           out.print(pp.getEmail() + ",");
           out.print(pp.getPhone() + "%%n");
           out.print("\r\n");
           out.print("\n\r");
           out.print("\n");
           out.print("\r");
           out.print("\\r\\n");
           out.print("\\n");
           out.print("\\n\\r");
           out.print(seperator);
           out.println('\n');
           out.println("%n");
           out.println("%%n");
        }

        //Close the output stream
        out.flush();
        out.close();
    }
    catch(Exception e) {
        e.printStackTrace();
    }

因此,这些都不起作用。 我的输出看起来像这样:

jeff,smith,jsmith @ email.com,555-123-1234 rick,smith,rsmith @ email.com,123-123-1233

应该看起来像:

jeff,smith,jsmith @ email.com,555-123-1234

里克,史密斯,rsmith @ email.com,123-123-1233

请不要发明自行车并使用Java CSV库。 它将消除您的所有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM