Timofei Davydik
Timofei Davydik

Reputation: 7304

Multiline text in Excel cells

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

Upvotes: 39

Views: 46117

Answers (3)

user1619768
user1619768

Reputation: 51

This worked for me

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

Upvotes: 0

Ed Norris
Ed Norris

Reputation: 4493

I found that you have to now include a third step after you follow the accepted answer to this question. You have to auto-size the column after you've added all of your data.

Assuming you're concerned about column 2,

sheet.autoSizeColumn(2);

See this example from Apache for context. It works for me with Java 8, poi-3.15.jar, and Excel for Mac.

Upvotes: 2

vikiiii
vikiiii

Reputation: 9476

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);

Upvotes: 61

Related Questions