Skip to content

Commit 0196438

Browse files
authored
BAEL-5204: Change the font color of a cell with Apache POI (eugenp#11554)
* BAEL-5204: First commit "Change the font color of a cell with Apache POI" * BAEL-5204: Create class and unit test * BAEL-5204: finalize unit test * BAEL-5204: fix indent * BAEL-5204: add missing excel template
1 parent bf4895c commit 0196438

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.poi.excel.cellstyle;
2+
3+
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
4+
import org.apache.poi.ss.usermodel.Cell;
5+
import org.apache.poi.ss.usermodel.CellStyle;
6+
import org.apache.poi.ss.usermodel.Font;
7+
import org.apache.poi.ss.usermodel.HorizontalAlignment;
8+
import org.apache.poi.ss.usermodel.VerticalAlignment;
9+
import org.apache.poi.ss.usermodel.Workbook;
10+
11+
public class CellStyler {
12+
public CellStyle createWarningColor(Workbook workbook) {
13+
CellStyle style = workbook.createCellStyle();
14+
15+
Font font = workbook.createFont();
16+
font.setFontName("Courier New");
17+
font.setBold(true);
18+
font.setUnderline(Font.U_SINGLE);
19+
font.setColor(HSSFColorPredefined.DARK_RED.getIndex());
20+
style.setFont(font);
21+
22+
style.setAlignment(HorizontalAlignment.CENTER);
23+
style.setVerticalAlignment(VerticalAlignment.CENTER);
24+
return style;
25+
}
26+
}
Binary file not shown.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.poi.excel.cellstyle;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.net.URISyntaxException;
6+
import java.nio.file.Paths;
7+
8+
import org.apache.poi.ss.usermodel.Cell;
9+
import org.apache.poi.ss.usermodel.CellStyle;
10+
import org.apache.poi.ss.usermodel.Row;
11+
import org.apache.poi.ss.usermodel.Sheet;
12+
import org.apache.poi.ss.usermodel.Workbook;
13+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
17+
public class CellStylerUnitTest {
18+
private static String FILE_NAME = "com/baeldung/poi/excel/cellstyle/CellStyle.xlsx";
19+
private static final String NEW_FILE_NAME = "CellStyleTest_output.xlsx";
20+
private String fileLocation;
21+
22+
@Before
23+
public void setup() throws IOException, URISyntaxException {
24+
fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME)
25+
.toURI())
26+
.toString();
27+
}
28+
29+
@Test
30+
public void testApplyWarningColor() throws IOException {
31+
Workbook workbook = new XSSFWorkbook(fileLocation);
32+
Sheet sheet = workbook.getSheetAt(0);
33+
Row row1 = sheet.createRow(0);
34+
row1.setHeightInPoints((short) 40);
35+
36+
CellStyler styler = new CellStyler();
37+
CellStyle style = styler.createWarningColor(workbook);
38+
39+
Cell cell1 = row1.createCell(0);
40+
cell1.setCellStyle(style);
41+
cell1.setCellValue("Hello");
42+
43+
Cell cell2 = row1.createCell(1);
44+
cell2.setCellStyle(style);
45+
cell2.setCellValue("world!");
46+
47+
FileOutputStream outputStream = new FileOutputStream(NEW_FILE_NAME);
48+
workbook.write(outputStream);
49+
outputStream.close();
50+
workbook.close();
51+
}
52+
}

0 commit comments

Comments
 (0)