2017-07-16 739 views
0

我想设置一些自定义(从十六进制或RGB值)颜色到一个xssfcell.But单元格的颜色变成黑色,即使我给一些其他颜色。我试图通过以下方式这样做:无法在XSSFCell中设置自定义颜色Apache POI

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); 

// END程序

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 

我试图在回答有关问题提到的其他许多方面,但这些都不解决我的问题。 请帮我一把。

+0

这是早上在这里,但1和3有什么区别?我认为你错过了一个填充模式,请参阅https://stackoverflow.com/a/31671032/180100 – 2017-07-16 09:00:03

+0

当然,我会用最少的代码更新这个问题。谢谢 – Pranjal

+0

填充模式在代码中并不缺少,如果填充图案丢失了,即使设置了颜色后,细胞仍然是全白的。但在我的情况下,全细胞是黑色的。 – Pranjal

回答

3

这是由Package org.apache.poi.ss.util的不完整引起的。

PropertyTemplate以及CellUtilRegionUtil是仅基于而不是xssf.usermodel水平ss.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(Color color)将在apache poi的更高版本中添加到org.apache.poi.ss.usermodel.CellStyle

+0

你可以在Apache POI bugzilla中提出这个改进请求,所以它不会被遗忘吗?补丁的奖励标记也是.... :) – Gagravarr

相关问题