Skip to content
Advertisement

Apache POI Excel row color is only black and doesn’t change

I am trying to make excel files background one row white and other aqua color. But for some reason whatever I do the color always changes to black.

private void writeTable(Table table, Row row, CellStyle style){
    if(row.getRowNum() % 2 == 0) {
        style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    }
    style.setWrapText(true);
    Cell cell = row.createCell(0);
    cell.setCellValue(table.index);
    cell.setCellStyle(style);

    //And it continues with other cells
}

It doesn’t change whatever I do, even if I try GREY_25_PERCENT its completely black. Here’s picture of my excel file

Advertisement

Answer

It may seem counterintuitive, but using

style.setFillPattern(CellStyle.SOLID_FOREGROUND);

in combination with

style.setFillForegroundColor(IndexedColors.AQUA.getIndex());

sets the background color of a cell.

The cell background itself probably also consists of two layers: a foreground and a background.

Advertisement