Can't Set Fill Color Apache POI Excel Workbook

Richie Episcopo picture Richie Episcopo · Jun 21, 2013 · Viewed 32.6k times · Source

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!

Answer

Sankumarsingh picture Sankumarsingh · Jun 22, 2013

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.