简单的POI背景填充问题

erg*_*aut 4 java excel apache-poi

我正在使用poi 3.14,我想要的是将单元格的背景设置为橙色.我不想要一个图案填充,只是一个坚实的橙色,但我不能让它工作.这就是我所拥有的:

    Row row = sheet.createRow(currentRow);

    CellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());
    style.setFillBackgroundColor(IndexedColors.ORANGE.getIndex());
    style.setFillPattern(CellStyle.ALIGN_FILL);

    Cell cell = row.createCell(0);
    cell.setCellValue("ABCDEFG");
    cell.setCellStyle(style);
Run Code Online (Sandbox Code Playgroud)

我最终得到的是带有small dots图案的橙色背景.在Excel的属性表上,它说我有bg=orange, fg=automatic, pattern=25% Gray.我怎样才能简单地使用空白图案样式?

Mus*_*afa 8

它首先似乎不直观,但该setFillForegroundColor方法实际上设置了背景填充的前景色而不是文本(首先想到的是前景).同样,您需要使用CellStyle.SOLID_FOREGROUND填充图案.请参阅下面的工作版本.

Row row = sheet.createRow(currentRow);

CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);

Cell cell = row.createCell(0);
cell.setCellValue("ABCDEFG");
cell.setCellStyle(style);
Run Code Online (Sandbox Code Playgroud)

有关更多示例,请参阅poi用户指南:http://poi.apache.org/spreadsheet/quick-guide.html#FillsAndFrills.

  • @ergonaut 不,POI 中的背景/前景不会混乱。微软是这种奇怪措辞的罪魁祸首:阴影可以由两种颜色(即“背景”*和*“前景”)+一个索引不同模式的值组成。Word 实现了基本相同的行为,请参阅[此处](http://stackoverflow.com/a/34544181/5717099)。 (2认同)