我正在尝试使用 Apache POI在.xlsx文件中获取单元格颜色信息。方法cellStyle.getFillBackgroundColor()返回很短。如何将 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>
1 回答
陪伴而非守候
TA贡献1757条经验 获得超8个赞
随着.XLSX电子表格,你可以调用的getFillBackgroundColorColor
(2“色”字)方法。它返回一个org.apache.poi.ss.usermodel.Color
(不是一个非常有用的接口),它XSSFColor
实现了。然后您可以将其转换为XSSFColor
.
XSSFColor = (XSSFColor) cellStyle.getFillBackgroundColorColor();
或者,再次使用 .xlxs 电子表格,您可以将 转换CellStyle
为XSSFCellStyle
,并且XSSFCellStyle
的getFillBackgroundColorColor
方法XSSFColor
直接返回一个。它也有getFillBackgroundXSSFColor
which 做同样的事情。
获取背景填充颜色。
注意 - 许多单元格实际上填充了前景填充,而不是背景填充 - 请参阅
getFillForegroundColor()
请注意,实心填充是作为前景色实现的,因此前景色可能是您真正想要的。前景色有互补的方法,例如getFillForegroundColorColor
。
添加回答
举报
0/150
提交
取消