aspose file tools*
The moose likes Java in General and the fly likes Line break in Excel thru Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Line break in Excel thru Java" Watch "Line break in Excel thru Java" New topic
Author

Line break in Excel thru Java

Murali Narayanaswamy
Greenhorn

Joined: May 29, 2007
Posts: 24
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
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
"<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.


Ping & DNS - my free Android networking tools app
Murali Narayanaswamy
Greenhorn

Joined: May 29, 2007
Posts: 24
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;}>");
 
 
subject: Line break in Excel thru Java