tags:

views:

386

answers:

1

I have a Standalone application in which I am trying to copy contents of JTable into Excel with line breaks included in my cells of JTable. I have used wrapping using "\"" to my cell contents. It's working fine but I am getting a square box type of symbol in place of line breaks in Excel. How to remove the symbol while copying? Is there any way to do this?

+1  A: 

Excel expects DOS/Windows line breaks ("\r\n") instead of Unix/Java ones (just "\n"). To fix this, use this code:

s = s.replace("\r\n", "\n").replace("\n", "\r\n");

The first part makes sure that you don't have any DOS/Win line breaks and then converts everything to DOS.

If you're stuck with Java 1.4, use replaceAll().

[EDIT] The square box means that there is an unprintable character in the string. I suggest to create a file with Excel which contains a line break in a cell and check which characters Excel uses.

Aaron Digulla
thank you for your reply..i tried by replacing using s.replace("\r\n", "\n").replace("\n", "\r\n"); but the boxes are still appearing in the excel.here is my code:S = (String) "\""+mProcessQuestionTestItemTable.getValueAt(rowsselected[i], colsselected[j])+"\"";S=S.replace("\r\n", "\n").replace("\n", "\r\n");stringbuffered.append(S);
Bharath
See my edits. .
Aaron Digulla
I had written code for pasting contents of Excel to JtableI tried as you said, i traced the contents of the excel,but i dint find any special character or symbol other than the "\n".In Excel,to go for the next line in cell,we need to press Alt+Enter.Do we need to catch that alt+Event and replace the \n's in jtable with these Alt+Enter events.If copy that squarebox and paste in another cell,alt+enter event is happening i.e a new line within in the cell.Can anyone help me out?Thank you
Bharath
No; try to create a file where every bit is the same as what Excel produces. In this case, remove the second `replace()` to make sure you only have `\n` in the output.
Aaron Digulla
I have created an excel file with line breaks(using Alt+Enter) in each and every cell and pasted it in a notepad file.I got the boxes now in notepad.when i copy the same(copied from excel) content from notepad to excel with boxes. Now the boxes are not visible in excel.can you suggest me how to get the value of this box and replace \n with it?i have tried alot but i cant find the solution.Please help me to solve this problem.
Bharath
any suggestions to remove the empty square box from excel(cloipboard) which is appearing before the line break in excel while pasting the JTable contents to Excel.
Bharath
Load the file in an editor which can show you the hex values of each character. http://en.wikipedia.org/wiki/Comparison_of_hex_editors
Aaron Digulla
Aaron Digulla