【问题标题】:Apache-POI : How to set background color of a cell when creating spreadsheet?Apache-POI:创建电子表格时如何设置单元格的背景颜色?
【发布时间】:2018-10-29 19:59:28
【问题描述】:

在 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 单元格的背景颜色?

【问题讨论】:

  • 你有没有想过如何为十六进制或 rgb 值执行此操作?
  • 不,我停止寻找它。我使用了@Vasif 的答案。

标签: java excel apache-poi


【解决方案1】:

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

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

【讨论】:

  • 感谢工作。如果我想使用十六进制值怎么办,例如“#AEAEAE”。有没有办法提供自定义值?
  • 也许您可以尝试使用IndexedColorMap 创建您的 rgb,但我从未使用过 style.setFillForegroundColor(new XSSFColor(new IndexedColorMap() { .....
猜你喜欢
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多