3

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

Jeredriq Demas
  • 616
  • 1
  • 9
  • 36

2 Answers2

2

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.

Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
0

Try using

style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

instead of

style.setFillPattern(CellStyle.SOLID_FOREGROUND);

It works with Apache POI 4.1.1