diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyleHandler.java b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyleHandler.java new file mode 100644 index 0000000000..4d97fe50cb --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyleHandler.java @@ -0,0 +1,30 @@ +package com.baeldung.poi.excel.cellstyle; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.FillPatternType; +import org.apache.poi.ss.usermodel.IndexedColors; + +public class CellStyleHandler { + + public void changeCellBackgroundColor(Cell cell) { + CellStyle cellStyle = cell.getCellStyle(); + if(cellStyle == null) { + cellStyle = cell.getSheet().getWorkbook().createCellStyle(); + } + cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + cell.setCellStyle(cellStyle); + } + + public void changeCellBackgroundColorWithPattern(Cell cell) { + CellStyle cellStyle = cell.getCellStyle(); + if(cellStyle == null) { + cellStyle = cell.getSheet().getWorkbook().createCellStyle(); + } + cellStyle.setFillBackgroundColor(IndexedColors.BLACK.index); + cellStyle.setFillPattern(FillPatternType.BIG_SPOTS); + cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + cell.setCellStyle(cellStyle); + } +} diff --git a/apache-poi/src/main/resources/cellstyle/CellStyleHandlerTest.xlsx b/apache-poi/src/main/resources/cellstyle/CellStyleHandlerTest.xlsx new file mode 100644 index 0000000000..29f128211b Binary files /dev/null and b/apache-poi/src/main/resources/cellstyle/CellStyleHandlerTest.xlsx differ diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java new file mode 100644 index 0000000000..e131db8e56 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStyleHandlerUnitTest.java @@ -0,0 +1,56 @@ +package com.baeldung.poi.excel.cellstyle; + +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Before; +import org.junit.Test; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URISyntaxException; +import java.nio.file.Paths; + +import static org.junit.Assert.assertEquals; + +public class CellStyleHandlerUnitTest { + private static final String FILE_NAME = "cellstyle/CellStyleHandlerTest.xlsx"; + private static final int SHEET_INDEX = 0; + private static final int ROW_INDEX = 0; + private static final int CELL_INDEX = 0; + + private String fileLocation; + private CellStyleHandler cellStyleHandler; + + @Before + public void setup() throws URISyntaxException { + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString(); + cellStyleHandler = new CellStyleHandler(); + } + + @Test + public void givenWorkbookCell_whenChangeCellBackgroundColor() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(SHEET_INDEX); + Row row = sheet.getRow(ROW_INDEX); + Cell cell = row.getCell(CELL_INDEX); + + cellStyleHandler.changeCellBackgroundColor(cell); + + assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor()); + workbook.close(); + } + + @Test + public void givenWorkbookCell_whenChangeCellBackgroundColorWithPattern() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(SHEET_INDEX); + Row row = sheet.getRow(ROW_INDEX); + Cell cell = row.getCell(CELL_INDEX + 1); + + cellStyleHandler.changeCellBackgroundColorWithPattern(cell); + + assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor()); + workbook.close(); + } +}