2017-07-16 737 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

相關問題