如何使用POI为XSSFCell设置自定义颜色?

4
我想在POI中为CellStyle设置自定义颜色,但似乎没有应用。我有以下代码:
    XSSFColor color = new XSSFColor(new byte[]{ (byte) 60,
                                                (byte) 120,
                                                (byte) 216 });
    
    CellStyle style = workBook.createCellStyle();
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setFillForegroundColor(color);
    
    XSSFCell cell = createCell(row, CellType.STRING, style, cellIndex);

我有以下的Gradle依赖:

implementation("org.apache.poi:poi:5.2.3")
implementation("org.apache.poi:poi-ooxml:5.2.3")

如果我使用不同的填充模式并执行以下操作:style.setFillPattern(FillPatternType.SOLID_FOREGROUND),则背景将被设置,但填充模式显然不是实心的。那么正确的做法是什么?

我已经更新了我的问题,并提供了这些细节。 - carlspring
2个回答

1

令我惊讶的是以下内容可行:

XSSFColor color = new XSSFColor(new byte[]{ (byte) 60,
                                            (byte) 120,
                                            (byte) 216 },
                                new DefaultIndexedColorMap());

CellStyle style = workBook.createCellStyle();
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFillForegroundColor(color);

XSSFCell cell = createCell(row, CellType.STRING, style, cellIndex);

指定一个new DefaultIndexedColorMap()似乎可以解决它!

0
你可以使用Java awt的Color类来实现这个功能。
 XSSFCellStyle cellStyle = this.createCellStyle(workbook);
 XSSFColor colorGrey = new XSSFColor(new Color(210, 210, 210));
 cellStyle.setFillForegroundColor(colorGrey);

没有填充图案,这行不通,对吧? - carlspring

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接