javaapache-poihssf

Change background color of row with Apache POI


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);

Solution

  • 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);
    }