簡體   English   中英

使用Apache POI設置Excel單元格的RGB背景色

[英]Set RGB background-color of an Excel cell with Apache POI

我正在嘗試將單元格從一行復制到下面的一行。 復制注釋和單元格值可以正常工作。 現在,如果我在Excel中設置了背景色,並且只想將其復制到新的單元格中,它將無法正常工作。 復制后僅沒有顏色,或者背景為黑色。 我嘗試過:

style2.setFillBackgroundColor(cell_master_alt.getCellStyle().getFillBackgroundColor());
style2.setFillForegroundColor(cell_master_alt.getCellStyle().getFillForegroundColor());

style2.setFillPattern(CellStyle.SOLID_FOREGROUND);    or
style2.setFillPattern(cell_master_alt.getCellStyle().getFillPattern());

cell_master_neu.setCellStyle(style2);

幾個小時沒有任何進展后,我查看了要復制的單元格的前景色和背景色。 在那里,我發現了RGB值。 現在,我想用該值創建一個新的Cellstyle,但效果不佳。 首先,我嘗試一下:

style_new.setFillForegroundColor(new XSSFColor(new java.awt.Color(128,128,128)));

但出現錯誤消息:

The method setFillForegroundColor(short) in the type CellStyle is not applicable for the arguments (XSSFColor)

怎么了

謝謝

在Apache POI中,由於Excel以二進制格式存儲單元格樣式,因此可以將其應用於多個單元格,因此只需將一個單元格的單元格樣式應用於另一個單元格而無需進行任何復制就足夠了:

cell_master_neu.setCellStyle(cell_master_alt.getCellStyle());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM