简体   繁体   中英

Writing comma separated numbers into one cell

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 ie "10001,10002,10003" -- did not worked 2) wrapping the comma separated list in "=""list""" ie "=""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. ie 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

Why you don't use another delimiter as cell delimiter, like ';'. In the import wizard of excel you can choose this delimiter as cell delimiter only. When excel use the currency formatter for the last column, you can force text type for this column.

Placing the text qualifier around your comma-separated string, and then IMPORT ing the file works as it should.

If the value is being split, be sure there is no space after that first comma (as there is in your example).

I will add some examples

Content of csv file

abc,"10001,10002,10003"

Import using Legacy Wizard

在此处输入图片说明

step 2

在此处输入图片说明

step 3

在此处输入图片说明

on the worksheet

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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