简体   繁体   中英

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

i am trying copy cells from one row to the one below. To copy the comments and cellvalues works fine. Now if I set a background-color in Excel and want to just copy it to the new cell, it doesn't work. Either there is just no color after copying or the background is black. I tried that:

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

After a few hours without any progress, I looked into the foreground- and background-color of the cell I want to copy. There I found RGB values. Now I want to make a new Cellstyle with that values, but that doesn't work as well. First I try this:

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

but there is an error message:

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

What is wrong with that?

Thanks

在Apache POI中,由于Excel以二进制格式存储单元格样式,因此可以将其应用于多个单元格,因此只需将一个单元格的单元格样式应用于另一个单元格而无需进行任何复制就足够了:

cell_master_neu.setCellStyle(cell_master_alt.getCellStyle());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM