Search code examples
javaxlscarriage-return

How to write a carriage return into xls file in Java


I would like to insert a carriage return into a cell of a xls file.
So I have written this code

address = rs.getString(16) + " " + rs.getString(17) + "
"
                                    + rs.getString(18) + " " + rs.getString(19) + " (" + 
                                    rs.getString(20) + ")";

"writer.write("<ss:Cell><ss:Data ss:Type=\"String\">" + address + "</ss:Data></ss:Cell>");`

but in the Excel File the result is that the carriage return is replaced with a "square symbol". In which mode can I resolve this issue?

Thanks, Stefano


Solution

  • In excel, to enter a new line in a cell, you need to insert ASCII characters 13 + 10 (constant CrLf on this page : http://msdn.microsoft.com/en-us/library/f63200h0%28v=vs.80%29.aspx).

    Have you tried:

    String crLf = Character.toString((char)13) + Character.toString((char)10);
    address = rs.getString(16) + " " + rs.getString(17) + crLf
                                + rs.getString(18) + " " + rs.getString(19) + " (" + 
                                rs.getString(20) + ")";