BAEL-6522: Expand Columns with Apache POI (#14801)
This commit is contained in:
parent
7af1c7917f
commit
e64dbf88ac
33
apache-poi-3/pom.xml
Normal file
33
apache-poi-3/pom.xml
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>apache-poi-3</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>apache-poi-3</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-ooxml</artifactId>
|
||||||
|
<version>${poi.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-scratchpad</artifactId>
|
||||||
|
<version>${poi.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<poi.version>5.2.3</poi.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
2
pom.xml
2
pom.xml
@ -816,6 +816,7 @@
|
|||||||
<module>apache-olingo</module>
|
<module>apache-olingo</module>
|
||||||
|
|
||||||
<module>apache-poi-2</module>
|
<module>apache-poi-2</module>
|
||||||
|
<module>apache-poi-3</module>
|
||||||
<module>apache-thrift</module>
|
<module>apache-thrift</module>
|
||||||
<module>apache-tika</module>
|
<module>apache-tika</module>
|
||||||
|
|
||||||
@ -1089,6 +1090,7 @@
|
|||||||
<module>apache-olingo</module>
|
<module>apache-olingo</module>
|
||||||
|
|
||||||
<module>apache-poi-2</module>
|
<module>apache-poi-2</module>
|
||||||
|
<module>apache-poi-3</module>
|
||||||
<module>apache-thrift</module>
|
<module>apache-thrift</module>
|
||||||
<module>apache-tika</module>
|
<module>apache-tika</module>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user