无法在XSSFCell Apache POI中设置自定义颜色

3

我正在尝试将一些自定义颜色(来自十六进制代码或RGB值)设置为xssfcell的颜色。但是,即使我给出其他颜色,单元格的颜色也会变成黑色。我已经尝试通过以下方式实现:

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

我尝试了许多其他问题答案中提到的方法,但都没有解决我的问题。请帮助我解决。


这里是早上,但是1和3之间有什么区别?我认为你缺少了一个填充图案,请参考https://stackoverflow.com/a/31671032/180100 - user180100
当然,我会更新问题并提供最少的代码。谢谢。 - NitSher01
代码中并没有缺少填充模式,即使填充模式缺失,单元格的颜色也会是完全白色。但在我的情况下,整个单元格都是黑色。 - NitSher01
请参考 https://stackoverflow.com/questions/43959440/apache-poi-fills-xssf-cell-with-black-instead-of-desired-custom-color-when-apply。问题是使用`RegionUtil`来设置边框,但是`org.apache.poi.ss.util.*`属于`SS`而不是`XSSF`,因此`SS`不能使用`XSSFColor`,只能使用颜色索引。 - Axel Richter
@AxelRichter:谢谢,现在它可以工作了。但是,我是否可以在我的代码中使用RegionUtil?因为我必须使用它来给表格添加外边框。 - NitSher01
显示剩余2条评论
2个回答

10
这是由于Package org.apache.poi.ss.util的不完整导致的。 PropertyTemplate以及CellUtilRegionUtil仅基于ss.usermodel级别,而非xssf.usermodel级别。但是,org.apache.poi.ss.usermodel.CellStyle至今仍不知道setFillForegroundColor(Color color)。它只知道setFillForegroundColor(short bg)。因此,直到现在,ss.usermodel级别无法设置Color作为填充前景色。只有short(颜色索引)是可能的。
如果问题是仅使用org.apache.poi.ss.util来设置边框,则需要设置颜色是必要的,答案是两者都包含在同一CellStyle中。因此,在向CellStyle添加边框设置时,必须保留并最终重新设置颜色设置。
因此,总之,这个困境没有出路。如果您需要使用org.apache.poi.ss.util,则同一时间不能使用setFillForegroundColor(XSSFColor color)。唯一的希望是在后续版本的apache poi中将setFillForegroundColor(Color color)添加到org.apache.poi.ss.usermodel.CellStyle

1
你能否在Apache POI Bugzilla上提出这个增强请求,以免被遗忘?如果能够提供一个补丁程序的话,额外加分。 :) - Gagravarr
2
三年过去了,这个问题仍然没有得到解决。:( - Andy
只是想让大家知道,这个问题已经在5.2.3版本中得到修复。 - gb.

0

作为一种解决方法,您可以使用条件格式设置自定义颜色,在设置了单元格样式的所有其他格式选项(对齐、边框等)之后。

这里是一个可行的(Kotlin)示例,定义了一种自定义颜色来区分偶数行和奇数行:

private fun setEvenOddColorFormatting(sheet: XSSFSheet) {
            val sheetConditionalFormatting = sheet.sheetConditionalFormatting

            val rule = sheetConditionalFormatting.createConditionalFormattingRule("MOD(ROW(), 2) = 0")
            val formatForRule = rule.createPatternFormatting()
            formatForRule.setFillBackgroundColor(XSSFColor(byteArrayOf(221.toByte(), 235.toByte(), 247.toByte())))
            formatForRule.fillPattern = PatternFormatting.SOLID_FOREGROUND

            val region = arrayOf(CellRangeAddress(0, sheet.lastRowNum,0,10))
            sheetConditionalFormatting.addConditionalFormatting(region, rule)
        }

一个缺点是,你必须将规则编写为Excel函数。但是你应该能够使用一个始终为真的函数并仅设置区域。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接