我試圖從一個XSLX文件獲取單元背景顏色並將此顏色設置爲另一個XSLX文件的單元背景時遇到問題。使用Apache POI將單元格背景顏色從一個XLSX文件錯誤應用到另一個
輸入和輸出文件都是2007(XLSX)格式(輸入文件是使用MS Excel 2010創建的)。
對於XLSX文件的工作,我使用Apache POI 3.14:在輸出文件
InputStream is = new FileInputStream("forRead.xlsx");
Workbook wb_in = WorkbookFactory.create(is);
XSSFCell cell_in = (XSSFCell) wb_in.getSheetAt(0).getRow(0).getCell(0);
XSSFColor background_in = ((XSSFColor) cell_in.getCellStyle().getFillForegroundColorColor());
XSSFColor background_out = new XSSFColor();
background_out.setARGBHex(background_in.getARGBHex()); // this works wrong :(.
XSSFWorkbook wb_out = new XSSFWorkbook();
Sheet sheet_out = wb_out.createSheet();
Row row_out = sheet_out.createRow((short) 2);
XSSFCell cell_out = (XSSFCell)row_out.createCell((short)1);
XSSFCellStyle style_out = wb_out.createCellStyle();
style_out.setFillForegroundColor(background_out); // if I use background_in, then the output color will be correct
style_out.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell_out.setCellStyle(style_out);
FileOutputStream fileOut = new FileOutputStream("forWrite.xlsx");
wb_out.write(fileOut);
fileOut.close();
結果色singnificantly不同。它更黑。 這是一個我不知道如何解決的問題。
在它旁邊,我注意到兩件怪事:
1)background_in.getARGBHex()
得到不同的顏色,我在Excel中看到。的#F79646 or rgb(247, 150, 70)
代替#fde9d9 or rgb(253, 233, 217)
2)顏色輸出文件將是正確的,如果我做style_out.setFillForegroundColor(background_in);
這讓我覺得,我可以通過設置像alpha
或別的東西XSSFColor對象某些除了性能解決propblem。可以下載我的Maven項目 from here。包含XLSX文件。
UPDATE:解決
我應該除了RGB設置色彩。