首页 > 解决方案 > Apache-POI:创建电子表格时如何设置单元格的背景颜色?

问题描述

在 Apache POI 4.0 中,我想设置一个 Excel 单元格背景颜色,如下所示:

IndexedColorMap colorMap = workbook.getStylesSource().getIndexedColors();
style.setFillForegroundColor(new XSSFColor(java.awt.Color.BLUE, colorMap).getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

    Row header = sheet.createRow(0);
    header.createCell(0).setCellValue("Title");
    header.getCell(0).setCellStyle(style);

..但我得到的只是黑色细胞。我尝试了很多东西,但结果总是一样的。

如何在 Apache POI 4.0 中设置 Excel 单元格的背景颜色?

标签: javaexcelapache-poi

解决方案


尝试使用以下代码作为背景样式

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


推荐阅读