From e64dbf88ac7ccc72a82b32a64f1614627c209c2c Mon Sep 17 00:00:00 2001 From: Manfred <77407079+manfred106@users.noreply.github.com> Date: Thu, 21 Sep 2023 18:24:00 +0100 Subject: [PATCH] BAEL-6522: Expand Columns with Apache POI (#14801) --- apache-poi-3/pom.xml | 33 +++++++++ .../expandcolumn/ExpandColumnUnitTest.java | 70 +++++++++++++++++++ pom.xml | 2 + 3 files changed, 105 insertions(+) create mode 100644 apache-poi-3/pom.xml create mode 100644 apache-poi-3/src/test/java/com/baeldung/poi/excel/expandcolumn/ExpandColumnUnitTest.java diff --git a/apache-poi-3/pom.xml b/apache-poi-3/pom.xml new file mode 100644 index 0000000000..5031e1c5c7 --- /dev/null +++ b/apache-poi-3/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + apache-poi-3 + 0.0.1-SNAPSHOT + apache-poi-3 + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.apache.poi + poi-ooxml + ${poi.version} + + + org.apache.poi + poi-scratchpad + ${poi.version} + + + + + 5.2.3 + + + \ No newline at end of file diff --git a/apache-poi-3/src/test/java/com/baeldung/poi/excel/expandcolumn/ExpandColumnUnitTest.java b/apache-poi-3/src/test/java/com/baeldung/poi/excel/expandcolumn/ExpandColumnUnitTest.java new file mode 100644 index 0000000000..04d0aef211 --- /dev/null +++ b/apache-poi-3/src/test/java/com/baeldung/poi/excel/expandcolumn/ExpandColumnUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.poi.excel.expandcolumn; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +import java.io.IOException; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ExpandColumnUnitTest { + + private Workbook workbook; + private Sheet sheet; + + @BeforeEach + void prepareSpreadsheet() { + workbook = new XSSFWorkbook(); + sheet = workbook.createSheet(); + + Row headerRow = sheet.createRow(0); + Cell headerCell1 = headerRow.createCell(0); + headerCell1.setCellValue("Full Name"); + Cell headerCell2 = headerRow.createCell(1); + headerCell2.setCellValue("Abbreviation"); + + Row dataRow = sheet.createRow(1); + Cell dataCell1 = dataRow.createCell(0); + dataCell1.setCellValue("Java Virtual Machine"); + Cell dataCell2 = dataRow.createCell(1); + dataCell2.setCellValue("JVM"); + + dataRow = sheet.createRow(2); + dataCell1 = dataRow.createCell(0); + dataCell1.setCellValue("Java Runtime Environment"); + dataCell2 = dataRow.createCell(1); + dataCell2.setCellValue("JRE"); + } + + @Test + void whenSetColumnWidth_thenColumnSetToTheSpecifiedWidth() { + + Row row = sheet.getRow(2); + String cellValue = row.getCell(0).getStringCellValue(); + int targetWidth = cellValue.length() * 256; + + sheet.setColumnWidth(0, targetWidth); + + assertEquals(targetWidth, sheet.getColumnWidth(0)); + } + + @Test + void whenAutoSizeColumn_thenColumnExpands() { + + int originalWidth = sheet.getColumnWidth(0); + + sheet.autoSizeColumn(0); + + assertThat(sheet.getColumnWidth(0)).isGreaterThan(originalWidth); + } + + @AfterEach + void cleanup() throws IOException { + workbook.close(); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index c65f6ce62d..060f158888 100644 --- a/pom.xml +++ b/pom.xml @@ -816,6 +816,7 @@ apache-olingo apache-poi-2 + apache-poi-3 apache-thrift apache-tika @@ -1089,6 +1090,7 @@ apache-olingo apache-poi-2 + apache-poi-3 apache-thrift apache-tika