Yiour
Yiour

Reputation: 187

Apache POI. style.setBackgroundColor is not working

Workbook workbook = new HSSFWorkbook();
Sheet worksheet = workbook.createSheet("test sheet");
Row row1 = worksheet.createRow(0);
Cell cell0  = row1.createCell(0);
CellStyle testStyle = workbook.createCellStyle();
testStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
cell0.setCellStyle(testStyle);

I`m trying to change background color of the cell to red. But actually the background color of the cell is not changing. It is still white

Upvotes: 0

Views: 2547

Answers (2)

krezus
krezus

Reputation: 1451

If @XtremeBaumer's solution does not work, after adding that line please change this line. from

testStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());

to

testStyle.setFillForegroundColor(IndexedColors.RED.getIndex());

If you use Apache Poi HSSF this one more suitable.

Upvotes: 1

XtremeBaumer
XtremeBaumer

Reputation: 6435

you are missing the line:

testStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

This will tell apache-poi how visible the color should be.

Upvotes: 5

Related Questions