首页 > 解决方案 > 如何在 apache poi 4.0 中将单元格的颜色设置为 RGB 值

问题描述

此处的示例无法编译。setFillForegroundColor 现在似乎需要一个索引,我可以欺骗它以显示特定的颜色,但是尝试任何指定 rgb 值的方法只会创建一个黑色单元格。

下面是 apache 自己的示例,它似乎与 4.0.0 过时了。 https://poi.apache.org/components/spreadsheet/quick-guide.html#CustomColors

XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell( 0);
cell.setCellValue("custom XSSF colors");

XSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);

标签: apache-poi

解决方案


推荐阅读