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

我试图设置一些自定义(从hex或rgb值)颜色到一个xssfcell.But单元格的颜色变成黑色,即使我给一些其他颜色。我已经尝试通过以下方式做到这一点:

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 

我尝试了许多其他方式来解答相关的问题,但没有一个解决了我的问题。 请帮我一下

这是由于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级别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