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

Nit*_*rma 1 java excel apache-poi xssf

我正在尝试为xssfcell设置一些自定义的颜色(从hexcode或rgb值开始),但是即使我提供了其他颜色,单元的颜色也正在变成黑色。我尝试通过以下方式进行操作:

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);
Run Code Online (Sandbox Code Playgroud)

//程序结束

   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
Run Code Online (Sandbox Code Playgroud)

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

Axe*_*ter 6

这是由于软件包org.apache.poi.ss.util不完整引起的。

PropertyTemplate以及CellUtilRegionUtil仅基于ss.usermodel级别而不是xssf.usermodel级别。但是org.apache.poi.ss.usermodel.CellStylesetFillForegroundColor(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)org.apache.poi.ss.usermodel.CellStyle在的更高版本中 添加到apache poi

  • 3年过去了,这个问题仍然没有得到解决。:( (2认同)