[英]How to use colors not in IndexedColors for Apache POI XSSF Excel?
I am looking at an excel sheet that I am wanting to duplicate and the only issue I am having is with colors.我正在查看我想要复制的 Excel 工作表,我遇到的唯一问题是颜色。 The colors I am looking to duplicate are Blue, Accent 5, Lighter 40%
and Light Green
from the Standard Colors
section.我希望复制的颜色是Standard Colors
部分中的Blue, Accent 5, Lighter 40%
和Light Green
。 I am looking at the docs for using custom colors in an XSSF Workbook and it states that the way to do it is like this:我正在查看在 XSSF 工作簿中使用自定义颜色的文档,它指出这样做的方法是这样的:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellValue("custom XSSF colors");
XSSFCellStyle style1 = wb.createCellStyle();
style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
When I try to use style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
当我尝试使用style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
I get an error, since the only parameter that .setFillForegroundColor()
only takes one parameter, and that is a short
and not an XSSFColor
.我收到一个错误,因为.setFillForegroundColor()
唯一参数只接受一个参数,而且它是一个short
而不是XSSFColor
。
Has anyone had any luck with this?有没有人有这方面的运气? I have been searching for hours and can't find anything that isn't 8 years old or doesn't work.我已经搜索了几个小时,但找不到任何不是 8 岁或不起作用的东西。
Using current apache poi 4.1.1
there is public void setFillForegroundColor(XSSFColor color) in XSSFCellStyle
.使用当前apache poi 4.1.1
有公共无效setFillForegroundColor(XSSFColor颜色)在XSSFCellStyle
。
The XSSFColor
should be created using constructor public XSSFColor(byte[] rgb, IndexedColorMap colorMap) since all other constructors are either deprecated or marked TEST ONLY
or not usable for creating a custom color. XSSFColor
应使用构造函数public XSSFColor(byte[] rgb, IndexedColorMap colorMap) 创建,因为所有其他构造函数要么已弃用,要么标记为TEST ONLY
或不可用于创建自定义颜色。
The RGB
values for the needed color can be got from Excel
by setting the color from the palette and then choose Fill Color
- More Colors
- Custom
.通过从调色板设置颜色,然后选择Fill Color
- More Colors
- Custom
可以从Excel
获取所需颜色的RGB
值。 Unfortunately apache poi
's IndexedColors are no more exact the colors of current Excel
versions.不幸的是apache poi
的IndexedColors不再是当前Excel
版本的颜色。 They are of version 2007
.它们是2007
版。 So they also can be used but later Excel
versions might show different colors then.所以它们也可以使用,但后来的Excel
版本可能会显示不同的颜色。
Complete example using current apache poi 4.1.1
:使用当前apache poi 4.1.1
完整示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap;
public class CreateExcelXSSFCellFillColor {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
java.util.List<XSSFCellStyle> cellStyles = new java.util.ArrayList<XSSFCellStyle>();
XSSFCellStyle cellStyle; byte[] rgb; XSSFColor color;
//Your custom color #800080
//create cell style on workbook level
cellStyle = workbook.createCellStyle();
//set pattern fill settings
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//create the RGB byte array
rgb = new byte[3];
rgb[0] = (byte) 128; // red
rgb[1] = (byte) 0; // green
rgb[2] = (byte) 128; // blue
//create XSSFColor
color = new XSSFColor(rgb, new DefaultIndexedColorMap());
//set fill color to cell style
cellStyle.setFillForegroundColor(color);
cellStyles.add(cellStyle);
//Light Green
cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
rgb = new byte[3];
rgb[0] = (byte) 146; // red
rgb[1] = (byte) 208; // green
rgb[2] = (byte) 80; // blue
color = new XSSFColor(rgb, new DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(color);
cellStyles.add(cellStyle);
//Blue, Accent 5, Lighter 40%
cellStyle = workbook.createCellStyle();
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
rgb = new byte[3];
rgb[0] = (byte) 155; // red
rgb[1] = (byte) 194; // green
rgb[2] = (byte) 230; // blue
color = new XSSFColor(rgb, new DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(color);
cellStyles.add(cellStyle);
Sheet sheet = workbook.createSheet();
for (int r = 0; r < cellStyles.size(); r++) {
Row row = sheet.createRow(r);
row.setHeight((short)(20*20));
Cell cell = row.createCell(0);
cell.setCellValue("cell style " + (r+1));
cell.setCellStyle(cellStyles.get(r));
}
sheet.setColumnWidth(0, 20*256);
FileOutputStream out = new FileOutputStream("CreateExcelXSSFCellFillColor.xlsx");
workbook.write(out);
out.close();
workbook.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.