[英]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.