1

在我的 Excel 文件中,一个宏会查找某些单元格值,然后更改填充颜色。

我拥有这些颜色的所有 RGB 值,并希望在我使用 Apache POI 在文件中写入数据时准确设置这些颜色。

我怎样才能做到这一点 ?

4

2 回答 2

3

我曾经遇到过一个问题,可能与您之前的问题相似。首先看一下这个例子,确定你知道为单元格填充颜色的方法:http:
//poi.apache.org/spreadsheet/quick-guide.html#CustomColors
看起来不错,但我发现了一个常见错误它很容易被拿走。如果您尝试在循环中设置单元格值和样式,则样式声明必须在每一轮循环中都是新鲜的。这意味着您必须在循环内重新初始化您的样式变量。这有点像你把这个声明放在你的循环中:

for(i=0;i<rowsize;i++){
    //
    //I suppose that we have an instance named row to working on.
    //
    XSSFCell cell = row.getCell(i);
    XSSFCellStyle style1 = wb.createCellStyle(); //create a fresh instance
    cell.setCellValue("custom XSSF colors");    //Set the cell value

    //This two line will setup the style of your cell with your needs
    style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
    style1.setFillPattern(CellStyle.SOLID_FOREGROUND);

    //Finally apply your style
    cell.setCellStyle(style1);
}
于 2012-11-03T05:09:55.637 回答
0

我知道在 POI 中添加自定义颜色的唯一方法是更改​​默认颜色:

Workbook wb = ...;    
wb.getCustomPalette().setColorAtIndex(HSSFColor.LIGHT_ORANGE.index, (byte) 255, (byte) 171, (byte) 115);
于 2012-07-25T07:35:19.910 回答