我正在尝试使用Apache POI获取.xlsx文件中单元格颜色信息。
cellStyle.getFillBackgroundColor()
方法返回short类型。我该如何将short转换为java.awt.Color
或其他格式(XSSFColor
)。
最终,我想根据单元格的背景颜色存储其值。
Workbook workbook = WorkbookFactory.create(new FileInputStream (new File(SAMPLE_XLSX_FILE_PATH)));
Sheet sheet = workbook.getSheetAt(0);
DataFormatter dataFormatter = new DataFormatter();
sheet.forEach(row -> {
row.forEach(cell -> {
String cellValue = dataFormatter.formatCellValue(cell);
CellStyle cellStyle = cell.getCellStyle();
System.out.println(cellStyle.getFillBackgroundColor());
//Color userColor = cellStyle.getFillBackgroundColor(); //ERROR
});
System.out.println();
});
我正在使用版本为3.6的软件,我认为该版本不支持getFillBackgroundColorColor()函数。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>