diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java new file mode 100644 index 0000000000..6d8b303fd3 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/cellstyle/CellStyler.java @@ -0,0 +1,26 @@ +package com.baeldung.poi.excel.cellstyle; + +import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined; +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.HorizontalAlignment; +import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.usermodel.Workbook; + +public class CellStyler { + public CellStyle createWarningColor(Workbook workbook) { + CellStyle style = workbook.createCellStyle(); + + Font font = workbook.createFont(); + font.setFontName("Courier New"); + font.setBold(true); + font.setUnderline(Font.U_SINGLE); + font.setColor(HSSFColorPredefined.DARK_RED.getIndex()); + style.setFont(font); + + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + return style; + } +} diff --git a/apache-poi/src/main/resources/com/baeldung/poi/excel/cellstyle/CellStyle.xlsx b/apache-poi/src/main/resources/com/baeldung/poi/excel/cellstyle/CellStyle.xlsx new file mode 100644 index 0000000000..ca9394246a Binary files /dev/null and b/apache-poi/src/main/resources/com/baeldung/poi/excel/cellstyle/CellStyle.xlsx differ diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStylerUnitTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStylerUnitTest.java new file mode 100644 index 0000000000..074e51919a --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/cellstyle/CellStylerUnitTest.java @@ -0,0 +1,52 @@ +package com.baeldung.poi.excel.cellstyle; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Paths; + +import org.apache.poi.ss.usermodel.Cell; +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.usermodel.XSSFWorkbook; +import org.junit.Before; +import org.junit.Test; + +public class CellStylerUnitTest { + private static String FILE_NAME = "com/baeldung/poi/excel/cellstyle/CellStyle.xlsx"; + private static final String NEW_FILE_NAME = "CellStyleTest_output.xlsx"; + private String fileLocation; + + @Before + public void setup() throws IOException, URISyntaxException { + fileLocation = Paths.get(ClassLoader.getSystemResource(FILE_NAME) + .toURI()) + .toString(); + } + + @Test + public void testApplyWarningColor() throws IOException { + Workbook workbook = new XSSFWorkbook(fileLocation); + Sheet sheet = workbook.getSheetAt(0); + Row row1 = sheet.createRow(0); + row1.setHeightInPoints((short) 40); + + CellStyler styler = new CellStyler(); + CellStyle style = styler.createWarningColor(workbook); + + Cell cell1 = row1.createCell(0); + cell1.setCellStyle(style); + cell1.setCellValue("Hello"); + + Cell cell2 = row1.createCell(1); + cell2.setCellStyle(style); + cell2.setCellValue("world!"); + + FileOutputStream outputStream = new FileOutputStream(NEW_FILE_NAME); + workbook.write(outputStream); + outputStream.close(); + workbook.close(); + } +}