Problem while copying JTable content in Excel
I have a standalone application where I am trying to copy content JTable
to Excel with line breaks included in my cells JTable
. I used a help wrapper "\""
for the content of my cell. It works fine, but I am getting a character type square square instead of line breaks in Excel. How do I remove a character while copying? Is there a way to do this?
source to share
Excel expects DOS / Windows line breaks ( "\r\n"
) instead of Unix / Java (just "\n"
). To fix it use this code:
s = s.replace("\r\n", "\n").replace("\n", "\r\n");
The first part makes sure you don't have DOS / Win line breaks and then converts everything to DOS.
If you are stuck with Java 1.4 use replaceAll()
.
[EDIT] A square square means there is an unprintable character in the string. I suggest creating a file with Excel that contains a line break in a cell and check what characters Excel is using.
source to share