Apache POI: new line without wrap

50 Views Asked by At

I want to write multiline text in one cell, but without wrapping them.

Expectation:

two lines - expectation

I start with simple:

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("First text line. \r\nNew line.");

first

It not works, but I found (1, 2, 3, 4) that I need setWrapText(true) to show new lines and set height of row:

Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("First text line. \r\nNew line.");
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
row.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints());

second

It works (visible new lines, height of two lines), but text is wrapped.

If I setWrapText(false), I'm back to where I started (new lines isn't visible):

third

How to show new lines, but don't wrap text?

Apache POI 5.2.4, Libre Office.

1

There are 1 best solutions below

1
Axel Richter On BEST ANSWER

Apache POI creates the Office Open XML file format and this file format is not able to store what your first screen shot shows - one cell having multiple text rows where the text nevertheless overflows the right cell border.

Libreoffice uses Open Document file format which is able to store what your first screen shot shows.

Create what your first screen shot shows using Libreoffice Calc. Then save that as *.xlsx (Libreoffice calls this Excel 2007-365-Format). Now close and reopen the file. You will see the overflow the right cell border is gone.

Now again create what your first screen shot shows using Libreoffice Calc. Then save that as *.ods (ODF-Format). Now close and reopen the file. You will get it exactly as got saved.

So the problem is with Office Open XML file format *.xlsx versus Open Document file format *.ods. And Apache POI is to create Office Open XML file format only.