我们如何获得 XSSFCell
的背景颜色
。我尝试使用 XSSFCellStyle
但没有成功。
FileInputStream fis = new FileInputStream(fileName);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
System.out.println(row.getCell(0).getCellStyle().getFillForegroundColor());
使用这些步骤我无法获得 Short
类型的背景颜色表示。
最佳答案
检查这个 URL:
https://issues.apache.org/bugzilla/show_bug.cgi?id=45492
Cell cell = row.getCell(1);
CellStyle cellStyle = cell.getCellStyle();
System.out.println("color = " + getColorPattern(cellStyle.getFillForegroundColor()));
private short[] getColorPattern(short colorIdx){
short[] triplet = null;
HSSFColor color = palette.getColor(colorIdx);
triplet = color.getTriplet();
System.out.println("color : " + triplet[0] +"," + triplet[1] + "," + triplet[2]);
return triplet;
}
这会返回 RGB 代码,但不是准确的代码。但与 XLS 自定义颜色选择器中的实际颜色代码相比,它或多或少返回了相同的颜色。
关于java - 如何使用 apache poi 获取单元格的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19491231/