Skip to content

Commit 5943875

Browse files
authored
BAEL-5202: Add excel multiline text (eugenp#11366)
* BAEL-5202: Add excel multiline text * BAEL-5202: Reformat code and rename ExcelMultilineText for consistency with unit test * Cleanup and formatting
1 parent 57473b1 commit 5943875

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.poi.excel.multilinetext;
2+
3+
import org.apache.poi.ss.usermodel.Cell;
4+
import org.apache.poi.ss.usermodel.CellStyle;
5+
import org.apache.poi.ss.usermodel.Row;
6+
7+
public class MultilineText {
8+
public void formatMultilineText(Cell cell, int cellNumber) {
9+
cell.getRow()
10+
.setHeightInPoints(cell.getSheet()
11+
.getDefaultRowHeightInPoints() * 2);
12+
CellStyle cellStyle = cell.getSheet()
13+
.getWorkbook()
14+
.createCellStyle();
15+
cellStyle.setWrapText(true);
16+
cell.setCellStyle(cellStyle);
17+
}
18+
}
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.poi.excel.multilinetext;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.io.File;
7+
import java.io.FileInputStream;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.net.URISyntaxException;
11+
import java.nio.file.Paths;
12+
13+
import org.apache.poi.ss.usermodel.Cell;
14+
import org.apache.poi.ss.usermodel.DataFormatter;
15+
import org.apache.poi.ss.usermodel.Row;
16+
import org.apache.poi.ss.usermodel.Sheet;
17+
import org.apache.poi.ss.usermodel.Workbook;
18+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
22+
public class MultilineTextUnitTest {
23+
private static String FILE_NAME = "com/baeldung/poi/excel/multilinetext/MultilineTextTest.xlsx";
24+
private static final String NEW_FILE_NAME = "MultilineTextTest_output.xlsx";
25+
private static final int STRING_ROW_INDEX = 1;
26+
private static final int STRING_CELL_INDEX = 0;
27+
28+
private String fileLocation;
29+
30+
@Before
31+
public void setup() throws IOException, URISyntaxException {
32+
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME)
33+
.toURI())
34+
.toString();
35+
}
36+
37+
@Test
38+
public void givenMultilineTextCell_whenFormated_thenMultilineTextVisible() throws IOException {
39+
Workbook workbook = new XSSFWorkbook(fileLocation);
40+
Sheet sheet = workbook.getSheetAt(0);
41+
sheet.createRow(STRING_ROW_INDEX);
42+
Row row = sheet.getRow(STRING_ROW_INDEX);
43+
Cell cell = row.createCell(STRING_CELL_INDEX);
44+
45+
cell.setCellValue("Hello \n world!");
46+
MultilineText multilineText = new MultilineText();
47+
multilineText.formatMultilineText(cell, STRING_CELL_INDEX);
48+
49+
FileOutputStream outputStream = new FileOutputStream(NEW_FILE_NAME);
50+
workbook.write(outputStream);
51+
outputStream.close();
52+
53+
File file = new File(NEW_FILE_NAME);
54+
FileInputStream fileInputStream = new FileInputStream(file);
55+
Workbook testWorkbook = new XSSFWorkbook(fileInputStream);
56+
assertTrue(row.getHeightInPoints() == testWorkbook.getSheetAt(0)
57+
.getRow(STRING_ROW_INDEX)
58+
.getHeightInPoints());
59+
DataFormatter formatter = new DataFormatter();
60+
assertEquals("Hello \n world!", formatter.formatCellValue(testWorkbook.getSheetAt(0)
61+
.getRow(STRING_ROW_INDEX)
62+
.getCell(STRING_CELL_INDEX)));
63+
testWorkbook.close();
64+
fileInputStream.close();
65+
file.delete();
66+
67+
workbook.close();
68+
}
69+
}

0 commit comments

Comments
 (0)