Skip to content

Commit d17820b

Browse files
author
Swapan Pramanick
committed
2 parents b2aaaea + 3efbc32 commit d17820b

136 files changed

Lines changed: 1595 additions & 1078 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apache-poi/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ This module contains articles about Apache POI
1212
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula)
1313
- [Setting Formulas in Excel with Apache POI](https://www.baeldung.com/java-apache-poi-set-formulas)
1414
- [Insert a Row in Excel Using Apache POI](https://www.baeldung.com/apache-poi-insert-excel-row)
15+
- [Multiline Text in Excel Cell Using Apache POI](https://www.baeldung.com/apache-poi-write-multiline-text)
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+
}

core-java-modules/core-java-11-2/pom.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,6 @@
3232
<artifactId>mockserver-junit-jupiter</artifactId>
3333
<version>${mockserver.version}</version>
3434
</dependency>
35-
<dependency>
36-
<groupId>org.junit.jupiter</groupId>
37-
<artifactId>junit-jupiter-engine</artifactId>
38-
<version>${junit-jupiter.version}</version>
39-
<scope>test</scope>
40-
</dependency>
41-
<dependency>
42-
<groupId>org.junit.jupiter</groupId>
43-
<artifactId>junit-jupiter-params</artifactId>
44-
<version>${junit-jupiter.version}</version>
45-
<scope>test</scope>
46-
</dependency>
47-
<dependency>
48-
<groupId>org.junit.jupiter</groupId>
49-
<artifactId>junit-jupiter-api</artifactId>
50-
<version>${junit-jupiter.version}</version>
51-
<scope>test</scope>
52-
</dependency>
5335
<dependency>
5436
<groupId>org.apache.commons</groupId>
5537
<artifactId>commons-lang3</artifactId>

core-java-modules/core-java-14/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@
2222
<version>${assertj.version}</version>
2323
<scope>test</scope>
2424
</dependency>
25-
<dependency>
26-
<groupId>org.junit.jupiter</groupId>
27-
<artifactId>junit-jupiter-engine</artifactId>
28-
<version>${junit-jupiter.version}</version>
29-
<scope>test</scope>
30-
</dependency>
31-
<dependency>
32-
<groupId>org.junit.jupiter</groupId>
33-
<artifactId>junit-jupiter-api</artifactId>
34-
<version>${junit-jupiter.version}</version>
35-
<scope>test</scope>
36-
</dependency>
3725
</dependencies>
3826

3927
<build>

core-java-modules/core-java-15/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,6 @@
2727
<version>${assertj.version}</version>
2828
<scope>test</scope>
2929
</dependency>
30-
<dependency>
31-
<groupId>org.junit.jupiter</groupId>
32-
<artifactId>junit-jupiter-engine</artifactId>
33-
<version>${junit-jupiter.version}</version>
34-
<scope>test</scope>
35-
</dependency>
36-
<dependency>
37-
<groupId>org.junit.jupiter</groupId>
38-
<artifactId>junit-jupiter-api</artifactId>
39-
<version>${junit-jupiter.version}</version>
40-
<scope>test</scope>
41-
</dependency>
4230
</dependencies>
4331

4432
<build>

core-java-modules/core-java-16/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,6 @@
2828
<artifactId>commons-lang3</artifactId>
2929
<version>3.12.0</version>
3030
</dependency>
31-
<dependency>
32-
<groupId>org.junit.jupiter</groupId>
33-
<artifactId>junit-jupiter-engine</artifactId>
34-
<version>${junit-jupiter.version}</version>
35-
<scope>test</scope>
36-
</dependency>
37-
<dependency>
38-
<groupId>org.junit.jupiter</groupId>
39-
<artifactId>junit-jupiter-api</artifactId>
40-
<version>${junit-jupiter.version}</version>
41-
<scope>test</scope>
42-
</dependency>
4331
</dependencies>
4432

4533
<build>

core-java-modules/core-java-17/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@
2323
<version>${assertj.version}</version>
2424
<scope>test</scope>
2525
</dependency>
26-
<dependency>
27-
<groupId>org.junit.jupiter</groupId>
28-
<artifactId>junit-jupiter-engine</artifactId>
29-
<version>${junit-jupiter.version}</version>
30-
<scope>test</scope>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.junit.jupiter</groupId>
34-
<artifactId>junit-jupiter-api</artifactId>
35-
<version>${junit-jupiter.version}</version>
36-
<scope>test</scope>
37-
</dependency>
3826
</dependencies>
3927

4028
<build>

core-java-modules/core-java-concurrency-2/pom.xml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
</parent>
1616

1717
<dependencies>
18-
<dependency>
19-
<groupId>org.junit.vintage</groupId>
20-
<artifactId>junit-vintage-engine</artifactId>
21-
<scope>test</scope>
22-
</dependency>
2318
<dependency>
2419
<groupId>com.googlecode.thread-weaver</groupId>
2520
<artifactId>threadweaver</artifactId>
@@ -31,6 +26,12 @@
3126
<artifactId>tempus-fugit</artifactId>
3227
<version>${tempus-fugit.version}</version>
3328
<scope>test</scope>
29+
<exclusions>
30+
<exclusion>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
</exclusion>
34+
</exclusions>
3435
</dependency>
3536
<dependency>
3637
<groupId>com.googlecode.multithreadedtc</groupId>

0 commit comments

Comments
 (0)