首页 文章

使用Apache POI设置Excel单元格的RGB背景颜色

提问于
浏览
0

我正在尝试将单元格从一行复制到下一行 . 复制注释和cellvalues工作正常 . 现在,如果我在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)

这有什么问题?

谢谢

1 回答

  • 3

    在Apache POI中,单元格样式可以应用于多个单元格,因为Excel以二进制格式存储它们,因此只需将单元格样式从一个单元格应用到另一个单元格而不进行任何复制即可:

    cell_master_neu.setCellStyle(cell_master_alt.getCellStyle());
    

相关问题