Jeredriq Demas
Jeredriq Demas

Reputation: 761

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

Upvotes: 3

Views: 2633

Answers (2)

user10018317
user10018317

Reputation:

Try using

style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

instead of

style.setFillPattern(CellStyle.SOLID_FOREGROUND);

It works with Apache POI 4.1.1

Upvotes: 0

Luciano van der Veekens
Luciano van der Veekens

Reputation: 6577

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.

Upvotes: 4

Related Questions