2012-07-25 130 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); 
}