gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:49:26 24 4
gpt4 key购买 nike

我正在尝试将单元格从一行复制到下面的一行。复制注释和单元格值效果很好。现在,如果我在 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());

关于java - 使用 Apache POI 设置 Excel 单元格的 RGB 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35410748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com