BAEL-5204: Change the font color of a cell with Apache POI (#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
This commit is contained in:
parent
c81fe2f74e
commit
480d9fb405
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue