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

标签 java excel apache-poi xssf

我正在尝试将一些自定义(从十六进制代码或 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

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

最佳答案

这是由于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)。唯一的希望是setFillForegroundColor(Color color)在以后的apache poi版本中加入org.apache.poi.ss.usermodel.CellStyle >.

关于java - 无法在 XSSFCell Apache POI 中设置自定义颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45126788/

相关文章:

Excel VBA 从超链接列表中提取 Web 数据

java - Apache POI : Indexed Color from AWT Color input

java - 如何使用java和apache poi删除Excel中的空单元格

用于显示容器内部件的 Java 图表库

java - 捕获内存不足错误之前的最后一个进程

java - 使用 GWT 查看文件是否被修改

java - 如何在 Lucene 3.0.2 中索引和搜索文本文件?

excel - 在多个 Excel 实例之一中查找工作簿

excel - 使用从文本文件导入的范围创建表

java - 使用 Apache-POI 库获取单元格内容时,我得到了 "Cannot get a numeric value from a text cell"和相反的结果。我如何解决它?