2012-07-25 114 views
1

在我的Excel文件中,宏查找特定的单元格值,然后更改fillcolor。使用Apache Poi复制Excel颜色

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

我该怎么做?

回答

0

我知道加入POI自定义颜色更改默认颜色的唯一方法:我曾遇到一个问题,可能之前类似于您的问题

Workbook wb = ...;  
wb.getCustomPalette().setColorAtIndex(HSSFColor.LIGHT_ORANGE.index, (byte) 255, (byte) 171, (byte) 115); 
+0

在POI 3.8有一个用于访问,也没有其他任何地方,我可以找到工作簿的调色板没有这样的方法。 – 2013-08-07 19:17:26

+0

对不起,我刚刚意识到该方法仅在HSSFWorkbook子类中定义。 – polypiel 2013-08-08 11:19:38

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); 
}