From 870628f03110f18ab99befb47ecab5de26a8cb35 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Sun, 21 Jan 2024 05:52:22 -0300 Subject: [PATCH] BAEL-7129 Apply Bold Text Style for an Entire Row Using Apache POI (#15633) * PR recreated due to repository truncation * review 1 * review 1 - 2 --- apache-poi-3/pom.xml | 2 +- .../com/baeldung/poi/rowstyle/PoiUtils.java | 53 +++++++ .../rowstyle/PoiBoldStyleIntegrationTest.java | 150 ++++++++++++++++++ 3 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 apache-poi-3/src/main/java/com/baeldung/poi/rowstyle/PoiUtils.java create mode 100644 apache-poi-3/src/test/java/com/baeldung/poi/rowstyle/PoiBoldStyleIntegrationTest.java diff --git a/apache-poi-3/pom.xml b/apache-poi-3/pom.xml index 1ac42f13a3..8d0204669f 100644 --- a/apache-poi-3/pom.xml +++ b/apache-poi-3/pom.xml @@ -76,7 +76,7 @@ - 5.2.3 + 5.2.5 4.1.1 0.15.7 2.6.12 diff --git a/apache-poi-3/src/main/java/com/baeldung/poi/rowstyle/PoiUtils.java b/apache-poi-3/src/main/java/com/baeldung/poi/rowstyle/PoiUtils.java new file mode 100644 index 0000000000..f40c3ad821 --- /dev/null +++ b/apache-poi-3/src/main/java/com/baeldung/poi/rowstyle/PoiUtils.java @@ -0,0 +1,53 @@ +package com.baeldung.poi.rowstyle; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Path; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +public class PoiUtils { + + private PoiUtils() { + } + + private static void newCell(Row row, String value) { + short cellNum = row.getLastCellNum(); + if (cellNum == -1) + cellNum = 0; + + Cell cell = row.createCell(cellNum); + cell.setCellValue(value); + } + + public static Row newRow(Sheet sheet, String... rowValues) { + Row row = sheet.createRow(sheet.getLastRowNum() + 1); + + for (String value : rowValues) { + newCell(row, value); + } + + return row; + } + + public static CellStyle boldFontStyle(Workbook workbook) { + Font boldFont = workbook.createFont(); + boldFont.setBold(true); + + CellStyle boldStyle = workbook.createCellStyle(); + boldStyle.setFont(boldFont); + + return boldStyle; + } + + public static void write(Workbook workbook, Path path) throws IOException { + try (FileOutputStream fileOut = new FileOutputStream(path.toFile())) { + workbook.write(fileOut); + } + } +} diff --git a/apache-poi-3/src/test/java/com/baeldung/poi/rowstyle/PoiBoldStyleIntegrationTest.java b/apache-poi-3/src/test/java/com/baeldung/poi/rowstyle/PoiBoldStyleIntegrationTest.java new file mode 100644 index 0000000000..dd031cf7cf --- /dev/null +++ b/apache-poi-3/src/test/java/com/baeldung/poi/rowstyle/PoiBoldStyleIntegrationTest.java @@ -0,0 +1,150 @@ +package com.baeldung.poi.rowstyle; + +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.streaming.SXSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.jupiter.api.Test; + +class PoiBoldStyleIntegrationTest { + + private void writeSampleSheet(Path destination, Workbook workbook) throws IOException { + Sheet sheet = workbook.createSheet(); + CellStyle boldStyle = PoiUtils.boldFontStyle(workbook); + + Row header = PoiUtils.newRow(sheet, "Name", "Value", "Details"); + header.setRowStyle(boldStyle); + + PoiUtils.newRow(sheet, "Albert", "A", "First"); + PoiUtils.newRow(sheet, "Jane", "B", "Second"); + PoiUtils.newRow(sheet, "Zack", "C", "Third"); + + PoiUtils.write(workbook, destination); + } + + private void assertRowStyleAppliedAndDefaultCellStylesDontMatch(Path sheetFile) throws IOException, InvalidFormatException { + try (Workbook workbook = new XSSFWorkbook(sheetFile.toFile())) { + Sheet sheet = workbook.getSheetAt(0); + Row row0 = sheet.getRow(0); + + XSSFCellStyle rowStyle = (XSSFCellStyle) row0.getRowStyle(); + assertTrue(rowStyle.getFont() + .getBold()); + + row0.forEach(cell -> { + XSSFCellStyle style = (XSSFCellStyle) cell.getCellStyle(); + assertNotEquals(rowStyle, style); + }); + + Row row1 = sheet.getRow(1); + XSSFCellStyle row1Style = (XSSFCellStyle) row1.getRowStyle(); + assertNull(row1Style); + + Files.delete(sheetFile); + } + } + + @Test + void givenXssfWorkbook_whenSetRowStyle1stRow_thenOnly1stRowStyled() throws IOException, InvalidFormatException { + Path sheetFile = Files.createTempFile("xssf-row-style", ".xlsx"); + + try (Workbook workbook = new XSSFWorkbook()) { + writeSampleSheet(sheetFile, workbook); + } + + assertRowStyleAppliedAndDefaultCellStylesDontMatch(sheetFile); + } + + @Test + void givenSxssfWorkbook_whenSetRowStyle_thenOnly1stRowStyled() throws IOException, InvalidFormatException { + Path sheetFile = Files.createTempFile("sxssf-row-style", ".xlsx"); + + try (Workbook workbook = new SXSSFWorkbook()) { + writeSampleSheet(sheetFile, workbook); + } + + assertRowStyleAppliedAndDefaultCellStylesDontMatch(sheetFile); + } + + @Test + void givenHssfWorkbook_whenSetRowStyle_thenOnly1stRowStyled() throws IOException { + Path sheetFile = Files.createTempFile("hssf-row-style", ".xls"); + + try (Workbook workbook = new HSSFWorkbook()) { + writeSampleSheet(sheetFile, workbook); + } + + try (Workbook workbook = new HSSFWorkbook(Files.newInputStream(sheetFile))) { + Sheet sheet = workbook.getSheetAt(0); + Row row0 = sheet.getRow(0); + + HSSFCellStyle rowStyle = (HSSFCellStyle) row0.getRowStyle(); + assertTrue(rowStyle.getFont(workbook) + .getBold()); + + row0.forEach(cell -> { + HSSFCellStyle style = (HSSFCellStyle) cell.getCellStyle(); + assertNotEquals(rowStyle, style); + }); + + Row row1 = sheet.getRow(1); + HSSFCellStyle row1Style = (HSSFCellStyle) row1.getRowStyle(); + assertNull(row1Style); + + Files.delete(sheetFile); + } + } + + @Test + void givenXssfWorkbook_whenSetCellStyleForEachRow_thenAllCellsContainStyle() throws IOException, InvalidFormatException { + Path sheetFile = Files.createTempFile("xssf-cell-style", ".xlsx"); + + try (Workbook workbook = new XSSFWorkbook()) { + Sheet sheet = workbook.createSheet(); + CellStyle boldStyle = PoiUtils.boldFontStyle(workbook); + + Row header = PoiUtils.newRow(sheet, "Name", "Value", "Details"); + header.forEach(cell -> cell.setCellStyle(boldStyle)); + + PoiUtils.newRow(sheet, "Albert", "A", "First"); + PoiUtils.newRow(sheet, "Jane", "B", "Second"); + PoiUtils.newRow(sheet, "Zack", "C", "Third"); + + PoiUtils.write(workbook, sheetFile); + } + + try (Workbook workbook = new XSSFWorkbook(sheetFile.toFile())) { + Sheet sheet = workbook.getSheetAt(0); + Row row0 = sheet.getRow(0); + + XSSFCellStyle rowStyle = (XSSFCellStyle) row0.getRowStyle(); + assertNull(rowStyle); + + row0.forEach(cell -> { + XSSFCellStyle style = (XSSFCellStyle) cell.getCellStyle(); + assertTrue(style.getFont() + .getBold()); + }); + + Row row1 = sheet.getRow(1); + rowStyle = (XSSFCellStyle) row1.getRowStyle(); + assertNull(rowStyle); + + Files.delete(sheetFile); + } + } +}