Multiline text in Excel cells

Timofei Davydik picture Timofei Davydik · Jul 11, 2012 · Viewed 37.7k times · Source

I'm trying to write multiline text to excel cells.

cell.setCellValue("line1 \n line2");

But when I open the document, I see only one line until I double-click it for editing, then it becomes two-lined. Why is it so? Thanks

Answer

vikiiii picture vikiiii · Jul 11, 2012

You need to set the row height to accomodate two lines of text.

row.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints()));

You need to set the wrap text = true to get the new line. Try this : Here wb is the Workbook.

 CellStyle cs = wb.createCellStyle();
 cs.setWrapText(true);
 cell.setCellStyle(cs);