当我尝试从一个XSLX文件获取单元格背景颜色并将此颜色设置为单元格的背景到另一个XSLX文件时,我遇到了问题 .
输入和输出文件均采用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();
输出文件中的结果颜色明显不同 . 它比较暗 . 这是一个我不知道如何解决的问题 .
除此之外,我注意到两件奇怪的事情:
1) background_in.getARGBHex()
获得了我在Excel中看到的不同颜色 . #F79646 or rgb(247, 150, 70)
而不是 #fde9d9 or rgb(253, 233, 217)
2)如果我这样做,输出文件中的颜色将是正确的 style_out.setFillForegroundColor(background_in);
这让我觉得我可以通过为XSSFColor对象设置一些附加属性,如 alpha
或其他东西来解决问题 .
我的Maven项目可以下载from here . 包含XLSX文件 .
UPDATE: SOLVED
我应该在RGB之外设置色调 .
1 回答
看来除了RGB之外我还应该添加色调属性的设置 . back_XSSFColor_out.setTint(back_XSSFColor_in.getTint());