无法在 XSSFCell Apache POI 中设置自定义颜色 [英] Not able to set custom color in XSSFCell Apache POI
问题描述
我正在尝试将一些自定义(来自 hexcode 或 rgb 值)颜色设置为 xssfcell.但是,即使我提供了其他颜色,单元格的颜色也变成了黑色.我尝试通过以下方式做到这一点:
I am trying to set some custom(from hexcode or rgb value) color to a xssfcell.But the color of the cell is becoming black even though I am giving some other color.I have tried doing this by the following ways :
File xlSheet = new File("C:\\Users\\IBM_ADMIN\\Downloads\\Excel Test\\Something3.xlsx");
System.out.println(xlSheet.createNewFile());
FileOutputStream fileOutISPR = new FileOutputStream("C:\\Users\\IBM_ADMIN\\Downloads\\Excel Test\\Something3.xlsx");
XSSFWorkbook isprWorkbook = new XSSFWorkbook();
XSSFSheet sheet = isprWorkbook.createSheet("TEST");
XSSFRow row = sheet.createRow(0);
XSSFCellStyle cellStyle = isprWorkbook.createCellStyle();
byte[] rgb = new byte[3];
rgb[0] = (byte) 24; // red
rgb[1] = (byte) 22; // green
rgb[2] = (byte) 219; // blue
XSSFColor myColor = new XSSFColor(rbg);
cellStyle.setFillForegroundColor(myColor);
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
XSSFCell cell = row.createCell(0);
cell.setCellValue("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has");
cell.setCellStyle(cellStyle);
CellRangeAddress rangeAddress = new CellRangeAddress(0, 0, 0, 2);
sheet.addMergedRegion(rangeAddress);
int width = ((int)(90 * 0.73)) * 256;
sheet.setColumnWidth(cell.getColumnIndex(), width);
//sheet.autoSizeColumn(cell.getColumnIndex());
RegionUtil.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM, rangeAddress, sheet, isprWorkbook);
RegionUtil.setBottomBorderColor(IndexedColors.RED.getIndex(), rangeAddress, sheet, isprWorkbook);
XSSFCell cell2 = row.createCell(11);
cell2.setCellValue("222222222222222");
isprWorkbook.write(fileOutISPR);
//程序结束
XSSFCellStyle cellStyle = isprWorkbook.createCellStyle();
byte[] rgb = new byte[3];
rgb[0] = (byte) 24; // red
rgb[1] = (byte) 22; // green
rgb[2] = (byte) 219; // blue
XSSFColor myColor = new XSSFColor(rgb);
cellStyle.setFillForegroundColor(myColor);//1st method
//cellStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));//2nd method
//XSSFColor myColor = new XSSFColor(Color.decode("0XFFFFFF"));
cellStyle.setFillForegroundColor(myColor);//3rd Method
我尝试了相关问题的答案中提到的许多其他方法,但没有一个能解决我的问题.请帮帮我.
I tried many other ways mentioned in answers to related questions but none of those solved my problem. Please help me out.
推荐答案
PropertyTemplate
以及 CellUtil
和 RegionUtil
仅基于 ss.usermodel
级别而不是基于xssf.usermodel
级别.但是 org.apache.poi.ss.usermodel.CellStyle 直到现在还不知道 setFillForegroundColor(Color color)
的一些事情.它只知道setFillForegroundColor(short bg)
.所以 ss.usermodel
级别直到现在还不能将 Color
设置为填充前景色.只有 short
(颜色索引)是可能的.
PropertyTemplate
as well as CellUtil
and RegionUtil
are be based on ss.usermodel
level only and not on xssf.usermodel
level. But org.apache.poi.ss.usermodel.CellStyle does not know something about a setFillForegroundColor(Color color)
until now. It only knows setFillForegroundColor(short bg)
. So ss.usermodel
level simply cannot set a Color
as fill foreground color until now. Only a short
(a color index) is possible.
如果谈到为什么只需要使用 org.apache.poi.ss.util
设置边框时需要设置颜色的问题,那么答案是,这是必要的,因为两者,颜色和边框,在同一个 CellStyle
中.这就是为什么在将边框设置添加到 CellStyle
时,必须保持颜色设置并最终重新设置.
If it comes to the question why setting the color is necessary when only the border shall be set using org.apache.poi.ss.util
then the answer is, it is necessary because both, color and border, are in the same CellStyle
. Thats why when adding the border settings to the CellStyle
, the color settings must be maintain and finally be set new.
所以总而言之,没有办法摆脱这种困境.如果你需要使用 org.apache.poi.ss.util
那么你不能同时使用 setFillForegroundColor(XSSFColor color)
.唯一的希望是 setFillForegroundColor(Color color)
将在 apache poi
org.apache.poi.ss.usermodel.CellStyle>.
So in conclusion, there is not a way out of this dilemma. If you need using org.apache.poi.ss.util
then you cannot use setFillForegroundColor(XSSFColor color)
the same time. The only hope is setFillForegroundColor(Color color)
will be added to org.apache.poi.ss.usermodel.CellStyle
in later versions of apache poi
.
这篇关于无法在 XSSFCell Apache POI 中设置自定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!