Skip to content

Commit f84ba03

Browse files
SeshuTechieSeshu Thanneeru
andauthored
Cell Background Color Change (eugenp#11400)
Co-authored-by: Seshu Thanneeru <[email protected]>
1 parent 756fa8d commit f84ba03

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.poi.excel.cellstyle;
2+
3+
import org.apache.poi.ss.usermodel.Cell;
4+
import org.apache.poi.ss.usermodel.CellStyle;
5+
import org.apache.poi.ss.usermodel.FillPatternType;
6+
import org.apache.poi.ss.usermodel.IndexedColors;
7+
8+
public class CellStyleHandler {
9+
10+
public void changeCellBackgroundColor(Cell cell) {
11+
CellStyle cellStyle = cell.getCellStyle();
12+
if(cellStyle == null) {
13+
cellStyle = cell.getSheet().getWorkbook().createCellStyle();
14+
}
15+
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
16+
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
17+
cell.setCellStyle(cellStyle);
18+
}
19+
20+
public void changeCellBackgroundColorWithPattern(Cell cell) {
21+
CellStyle cellStyle = cell.getCellStyle();
22+
if(cellStyle == null) {
23+
cellStyle = cell.getSheet().getWorkbook().createCellStyle();
24+
}
25+
cellStyle.setFillBackgroundColor(IndexedColors.BLACK.index);
26+
cellStyle.setFillPattern(FillPatternType.BIG_SPOTS);
27+
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
28+
cell.setCellStyle(cellStyle);
29+
}
30+
}
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.poi.excel.cellstyle;
2+
3+
import org.apache.poi.ss.usermodel.*;
4+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.io.OutputStream;
11+
import java.net.URISyntaxException;
12+
import java.nio.file.Paths;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
public class CellStyleHandlerUnitTest {
17+
private static final String FILE_NAME = "cellstyle/CellStyleHandlerTest.xlsx";
18+
private static final int SHEET_INDEX = 0;
19+
private static final int ROW_INDEX = 0;
20+
private static final int CELL_INDEX = 0;
21+
22+
private String fileLocation;
23+
private CellStyleHandler cellStyleHandler;
24+
25+
@Before
26+
public void setup() throws URISyntaxException {
27+
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME).toURI()).toString();
28+
cellStyleHandler = new CellStyleHandler();
29+
}
30+
31+
@Test
32+
public void givenWorkbookCell_whenChangeCellBackgroundColor() throws IOException {
33+
Workbook workbook = new XSSFWorkbook(fileLocation);
34+
Sheet sheet = workbook.getSheetAt(SHEET_INDEX);
35+
Row row = sheet.getRow(ROW_INDEX);
36+
Cell cell = row.getCell(CELL_INDEX);
37+
38+
cellStyleHandler.changeCellBackgroundColor(cell);
39+
40+
assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor());
41+
workbook.close();
42+
}
43+
44+
@Test
45+
public void givenWorkbookCell_whenChangeCellBackgroundColorWithPattern() throws IOException {
46+
Workbook workbook = new XSSFWorkbook(fileLocation);
47+
Sheet sheet = workbook.getSheetAt(SHEET_INDEX);
48+
Row row = sheet.getRow(ROW_INDEX);
49+
Cell cell = row.getCell(CELL_INDEX + 1);
50+
51+
cellStyleHandler.changeCellBackgroundColorWithPattern(cell);
52+
53+
assertEquals(IndexedColors.LIGHT_BLUE.index, cell.getCellStyle().getFillForegroundColor());
54+
workbook.close();
55+
}
56+
}

0 commit comments

Comments
 (0)