I have scanned this forum over and over and tried every method mentioned on here and still can't get Apache POI to change to fill background color of my excel document.
Here is my code:
errorOccured = true; XSSFCellStyle cs = workbook.createCellStyle(); cs.setFillBackgroundColor(IndexedColors.RED.getIndex()); row.getCell(0).setCellStyle(cs);
Do you know why this wouldn't work? What is the correct way to get row.getCell(0)
to be filled with red (background color)?
Thank you!
Apache POI provides three methods for changing the background color. In the CellStyle class, we can use the setFillForegroundColor, setFillPattern, and setFillBackgroundColor methods for this purpose. A list of colors is defined in the IndexedColors class. Similarly, a list of patterns is defined in FillPatternType.
Code ExplanationXSSFCellStyle style=workbook. createCellStyle(); Then style the background and foreground by setFillBackgroundColor and setFillPattern, then give the cell value and cell style.
Apache POI is your Java Excel solution (for Excel 97-2008). We have a complete API for porting other OOXML and OLE2 formats and welcome others to participate. OLE2 files include most Microsoft Office files such as XLS, DOC, and PPT as well as MFC serialization API based file formats.
Apache POI provides three methods for changing the background color. In the CellStyle class, we can use the setFillForegroundColor, setFillPattern, and setFillBackgroundColor methods for this purpose.
Add the Apache POI and Apache-ooxml dependency into the POM.xml file After installing all the dependencies, we are now ready to write the code, it is styled using the creatcellstyle method. The style method provides many designs such as setFillBackgroundColor, setFillForegroundColor and setFillPattern.
The style method provides many designs such as setFillBackgroundColor, setFillForegroundColor and setFillPattern. This method enables us to set the background color of the cell, the Apache POI dependency provides us with the Indexed color class that has all the colors.
Setting color can be used as heading or column name that increases the readability of excel file. In this example we will understand from scratch how to color in XLSX. All we need is to get an instance of CellStyle and then set the desired color to CellStyle and then assign it to XLSX cell.
Use foreground color instead of Background color.
errorOccured = true; XSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.RED.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); row.getCell(0).setCellStyle(style);
this will fill the cell background color with RED.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With