简体   繁体   English

如何设置单元格的背景色?

[英]How to set the background color of a cell?

I am trying to set the background color of a cell on a excel sheet. 我正在尝试在Excel工作表上设置单元格的背景色。 I can't get the XSSFColor to work. 我无法使用XSSFColor This is what I have so far: 这是我到目前为止的内容:

XSSFCellStyle style = (XSSFCellStyle)workbook.createCellStyle();
byte[] rgb = new byte[3];
rgb[0] = (byte) 100;
rgb[1] = (byte) 150;
rgb[2] = (byte) 200;
XSSFColor color = new XSSFColor(rgb);
style.setFillForegroundColor(color);

The line XSSFColor color = new XSSFColor(rgb); 该行XSSFColor color = new XSSFColor(rgb); does not compile: 无法编译:

在此处输入图片说明

How can I create a custom colored XSSFColor from RGB or Hex? 如何从RGB或Hex创建自定义的彩色XSSFColor

You should use XSSFColor(byte[] rgb, IndexedColorMap colorMap) constructor to create color instance. 您应该使用XSSFColor(byte[] rgb, IndexedColorMap colorMap)构造函数创建颜色实例。 You may try to set colorMap as null here: 您可以尝试在此处将colorMap设置为null

XSSFColor color = new XSSFColor(rgb, null);

Or use contructor without parameters and set rgb later: 或者使用不带参数的构造器,并在以后设置rgb:

XSSFColor color = new XSSFColor();
color.setRGB(rgb);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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