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

时间:2013-04-10 15:06:02

标签: java csv printwriter

我已经尝试过在网上和这个特定网站上找到的所有内容,但这些选项都没有效果。我循环遍历对象列表并创建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();
    }

所以这些都没有奏效。我的输出看起来像这样:

  

杰夫,史密斯,jsmith的@ email.com,555-123-1234   瑞克,史密斯,rsmith @ email.com,123-123-1233

应该是这样的:

  

杰夫,史密斯,jsmith的@ email.com,555-123-1234

     

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

1 个答案:

答案 0 :(得分:1)

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