I am generating CSV file using Java code with header fields say UserName, AccountNumbers. Now for AccountNumber header field, I need to show all account numbers as comma separated in onc cell only.
ex. abc, 10001,10002,10003
Now when I open the generated CSV file in excel, the account numbers are not displaying properly and that cell value is considered as Number, and by default Number format getting applied when opening in excel.
I applied below solutions :
1) wrapping the comma separated list in double quotes i.e. "10001,10002,10003" -- did not worked 2) wrapping the comma separated list in "=""list""" i.e. "=""10001,10002,10003""" -- problem with this wrapping is that, for some of the list it's working fine, but for some, I am getting "= appearing in start of the list. i.e. some list are already considered as text format (not sure why)
HttpServletResponse response = this.getPortalPageContext().getResponse();
response.setContentType("text/csv");
response.setHeader("Content-Disposition", "attachment;filename=user_search_export.csv");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "cache");
response.setHeader("Cache-Control", "must-revalidate");
Writer out = response.getWriter();
StringBuffer tmp = new StringBuffer();
tmp.append("UserName");
tmp.append("Accounts");
out.write(tmp.toString());
out.write("\r\n");
StringBuffer tmp = new StringBuffer();
String accountNo = "10001,10002,10003";
tmp.append("abc");
tmp.append("\"=\"\"" + accountNo + "\"\"\"");
out.write(tmp.toString());
out.write("\r\n");
Thanks