0

我已经尝试了几乎所有我可以在网上和这个特定网站上找到的东西,但这些选项都不起作用。我正在遍历对象列表并创建一个 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

应该看起来像:

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

rick,smith,rsmith@email.com,123-123-1233

4

1 回答 1

1

请不要发明自行车并使用Java CSV图书馆。它会消除你所有的问题。

于 2013-04-10T15:08:49.637 回答