Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Line break in Excel thru Java

 
Murali Narayanaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am getting values from DB and preparing string like below

String UpdatedComments = userId + " "+ formattedDate + "\n" + Comments
+ "\n" + old_comments + "\r\n";
In the browser it is coming properly For Eg

Admin 06/26/2008
comments New
Comments old

But when I export to Excel, it is displaying as(everything in a single line)
Admin 06/26/2008 comments New Comments old

I tried to replace "\n" with <br> when exporting to excel but it is not working as expected

1) comments = comments.replaceAll("\n","<br>");

2)comments = comments.replaceAll("<","<");

3)comments = comments.replaceAll(">",">");

2 AND 3 ARE WORKING AS EXPECTED.

Pls let me know what is to be replaced for "\n" for a line break in excel.

Thanks much
Murali
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"<br>" is an HTML thing; you shouldn't expect Excel to know what to do with it.

Two things to try might be: use \r\n instead of \n, and give the file a ".csv" extension so that Excel knows it has its data organized in rows.
 
Murali Narayanaswamy
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input.
I had to change a little bit to get to working

comments = comments.replaceAll("<","<");
comments = comments.replaceAll(">",">");
comments = comments.replaceAll("\n","<br {mso-data-placement:same-cell;}>");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic