Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 721
Timofei Davydik Avatar asked Jul 11 '12 08:07

Timofei Davydik


People also ask

How do you write long sentences in one cell in Excel?

To start a new line of text at any specific point in a cell: Double-click the cell in which you want to enter a line break. Tip: You can also select the cell, and then press F2. In the cell, click the location where you want to break the line, and press Alt + Enter.

How can you automatically wrap multiple lines of text in a cell?

Using automatic wrapping To set text to wrap at the end of the cell, right-click on the cell and select Format Cells (or choose Format > Cells from the menu bar, or press Ctrl+1). On the Alignment tab, under Properties, select Wrap text automatically.


1 Answers

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); 
like image 81
vikiiii Avatar answered Sep 18 '22 12:09

vikiiii