2017-05-14 43 views
0

當我寫這應該使一些細胞填充橙色的Excel電子表格程序,給定的文本,並給予使用邊界PropertyTemplate。我已經成功地編寫了代碼在HSSF中執行此操作,但是我現在正在處理XSSF並且無法使其工作。的Apache POI充滿XSSF細胞與黑色的,而不是期望的自定義​​顏色應用PropertyTemplate邊界

正在發生的事情是細胞越來越充滿了正確的橙色,和文本進入細胞正常爲好,但應用PropertyTemplate使得橙細胞變成黑色。有沒有人知道解決這個問題的方法?

這裏是我的代碼。

XSSFCellStyle orangeFillStyle = wb.createCellStyle(); 
orangeFillStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(255, 192, 0))); 
orangeFillStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); 

然後還有一大堆代碼/算法來找出填補橙色的細胞,並把這些細胞的文字在我使用的代碼突出顯示單元格:

currCell.setCellStyle(orangeFillStyle); 

在很頁生成的到底是PropertyTemplate(境)代碼:

BorderStyle dividerStyle = BorderStyle.THIN; 
PropertyTemplate borderTemplate = new PropertyTemplate(); 
borderTemplate.drawBorders(new CellRangeAddress(0, 0, 0, 13), dividerStyle, BorderExtent.BOTTOM); 
borderTemplate.drawBorders(new CellRangeAddress(0, 0, 1, 13), dividerStyle, BorderExtent.TOP); 
borderTemplate.drawBorders(new CellRangeAddress(0, rowI, 1, 13), dividerStyle, BorderExtent.VERTICAL); 
borderTemplate.drawBorders(new CellRangeAddress(rowI, rowI, 0, 13), dividerStyle, BorderTextent.BOTTOM); 
borderTemplate.applyBorders(sheet); 

如果我註釋掉borderTemplate.applyBorders(sheet);線,填充顏色看起來只有精細。但是,如果我這樣做,顯然我的表格上沒有任何邊框。這就像我不能同時具有自定義填充顏色和邊框一樣。有誰知道爲什麼這可能會發生或繞過它?

我會注意到,如果我使用IndexedColor而不是自定義顏色,然後填充和邊框工作得很好。唯一的問題是我不喜歡任何索引顏色。下面是我用IndexedColor代碼:

XSSFCellStyle orangeFillStyle = wb.createCellStyle(); 
orangeFillStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); 
orangeFillStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); 

回答

0

嗯,看起來像它與Apache POI的錯誤。我注意到有人在不久之前在其錯誤跟蹤器中發佈了它,它仍然存在於3.16版本中。

萬一別人遇到這個問題,我的解決方法是填充任何細胞,我的自定義橙色之前把PropertyTemplate(邊界)上。這意味着顯着修改我的代碼,但它現在可以工作。

+0

這不是一個錯誤,但不完整。 'PropertyTemplate'和'CellUtil'僅僅基於'ss.usermodel'級別,而不是'xssf.usermodel'級別。但是,[org.apache.poi.ss.usermodel.CellStyle(https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/CellStyle.html)不知道有一個什麼'setFillForegroundColor(顏色)'。它只知道'setFillForegroundColor(short bg)'。因此,ss.usermodel級別直到現在根本無法將「顏色」設置爲填充前景色。只有一個「短」(一個顏色指數)是可能的。 –

相關問題