I am trying to get cell color info in .xlsx file using Apache POI.
我试图使用Apache POI在.xlsx文件中获取单元格颜色信息。
Method cellStyle.getFillBackgroundColor()
is returning short. How can I convert short to java.awt.Color
or any other format(XSSFColor
).
方法cellStyle.getFillBackgroundColor()返回short。如何将short转换为java.awt.Color或任何其他格式(XSSFColor)。
Ultimately I want to store the value of cell based on its background color.
最终我想根据背景颜色存储单元格的值。
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();
});
I am using version 3.6 which I think do not support getFillBackgroundColorColor()
我使用的是版本3.6,我认为不支持getFillBackgroundColorColor()
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.6</version>
</dependency>
1 个解决方案
#1
4
With .xlsx spreadsheets, you can call the getFillBackgroundColorColor
(2 "Color" words) method. It returns an org.apache.poi.ss.usermodel.Color
(not a very useful interface), that XSSFColor
implements. Then you can cast it as XSSFColor
.
使用.xlsx电子表格,您可以调用getFillBackgroundColorColor(2“Color”字样)方法。它返回一个XSSFColor实现的org.apache.poi.ss.usermodel.Color(不是一个非常有用的接口)。然后你可以把它作为XSSFColor。
XSSFColor = (XSSFColor) cellStyle.getFillBackgroundColorColor();
Alternatively, again with .xlxs spreadsheets, you can cast the CellStyle
as an XSSFCellStyle
, and XSSFCellStyle
's getFillBackgroundColorColor
method returns an XSSFColor
directly. It also has getFillBackgroundXSSFColor
which does the same thing.
或者,再次使用.xlxs电子表格,您可以将CellStyle转换为XSSFCellStyle,XSSFCellStyle的getFillBackgroundColorColor方法直接返回XSSFColor。它也有getFillBackgroundXSSFColor,它做同样的事情。
Get the background fill color.
获取背景填充颜色。
Note - many cells are actually filled with a foreground fill, not a background fill - see
getFillForegroundColor()
注意 - 许多单元格实际上填充了前景填充,而不是背景填充 - 请参阅getFillForegroundColor()
Beware that solid fills are implemented as foreground colors, so the foreground color may be what you're really after. There are complementary methods for the foreground color, e.g. getFillForegroundColorColor
.
请注意,实心填充是作为前景色实现的,因此前景色可能是您真正想要的。前景色有补充方法,例如: getFillForegroundColorColor。
#1
4
With .xlsx spreadsheets, you can call the getFillBackgroundColorColor
(2 "Color" words) method. It returns an org.apache.poi.ss.usermodel.Color
(not a very useful interface), that XSSFColor
implements. Then you can cast it as XSSFColor
.
使用.xlsx电子表格,您可以调用getFillBackgroundColorColor(2“Color”字样)方法。它返回一个XSSFColor实现的org.apache.poi.ss.usermodel.Color(不是一个非常有用的接口)。然后你可以把它作为XSSFColor。
XSSFColor = (XSSFColor) cellStyle.getFillBackgroundColorColor();
Alternatively, again with .xlxs spreadsheets, you can cast the CellStyle
as an XSSFCellStyle
, and XSSFCellStyle
's getFillBackgroundColorColor
method returns an XSSFColor
directly. It also has getFillBackgroundXSSFColor
which does the same thing.
或者,再次使用.xlxs电子表格,您可以将CellStyle转换为XSSFCellStyle,XSSFCellStyle的getFillBackgroundColorColor方法直接返回XSSFColor。它也有getFillBackgroundXSSFColor,它做同样的事情。
Get the background fill color.
获取背景填充颜色。
Note - many cells are actually filled with a foreground fill, not a background fill - see
getFillForegroundColor()
注意 - 许多单元格实际上填充了前景填充,而不是背景填充 - 请参阅getFillForegroundColor()
Beware that solid fills are implemented as foreground colors, so the foreground color may be what you're really after. There are complementary methods for the foreground color, e.g. getFillForegroundColorColor
.
请注意,实心填充是作为前景色实现的,因此前景色可能是您真正想要的。前景色有补充方法,例如: getFillForegroundColorColor。