Change background color of row with Apache POI

5.7k Views Asked by At

I try to use Apache POI to change background colors of cells in a row. I use following code to handle it in xls file, but there aren't any changes in file after execution.

FileInputStream fis = new FileInputStream(src);
HSSFWorkbook wb = new HSSFWorkbook(fis);                
r = sheet.getRow(5);
CellStyle style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
r.setRowStyle(style);
1

There are 1 best solutions below

0
plaidshirt On BEST ANSWER

Style for cells has to be defined like this.

HSSFCellStyle tCs = wb.createCellStyle();
tCs.setFillPattern(FillPatternType.SOLID_FOREGROUND);
tCs.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

It has to be applied each cells, which needed this style.

for (int k = 0; k < sheet.getRow(5).getLastCellNum(); k++) {
   sheet.getRow(i).getCell(k).setCellStyle(tCs);
}