Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Set Fill Color Apache POI Excel Workbook

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!

like image 390
Richie Episcopo Avatar asked Jun 21 '13 20:06

Richie Episcopo


People also ask

How do I add color to a cell in Apache POI?

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.

How do you fill color in Excel in Java?

Code ExplanationXSSFCellStyle style=workbook. createCellStyle(); Then style the background and foreground by setFillBackgroundColor and setFillPattern, then give the cell value and cell style.

Does Apache POI support XLS?

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.

How to change the background color of the cell in Apache POI?

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.

How to style a POI using Apache POI and OOXML?

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.

How to set the background color of the cell in Excel?

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.

How to set color in xlsx?

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.


1 Answers

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.

like image 105
Sankumarsingh Avatar answered Sep 20 '22 05:09

Sankumarsingh