Java 没有背景单元格的HSSFColor返回错误的背景颜色

Java 没有背景单元格的HSSFColor返回错误的背景颜色,java,apache-poi,Java,Apache Poi,我正在我的项目中使用ApachePOI3.9。我试图读取HSSF对象excel单元格,并从中获取背景色 Workbook myWorkBook = WorkbookFactory.create(new File(filePath)); Sheet mySheet = myWorkBook.getSheetAt(0); Row currentRow = null; Iterator<Row> rowIterator = mySheet.iterator(); while (rowIt

我正在我的项目中使用ApachePOI3.9。我试图读取HSSF对象excel单元格,并从中获取背景色

Workbook myWorkBook = WorkbookFactory.create(new File(filePath));
Sheet mySheet = myWorkBook.getSheetAt(0);

Row currentRow = null;
Iterator<Row> rowIterator = mySheet.iterator();
while (rowIterator.hasNext())
{
currentRow = (Row) rowIterator.next();
totalColumns = currentRow.getPhysicalNumberOfCells();

for (int column = 0; column < totalColumns; column++)
{
Cell cell = currentRow.getCell(column);
CellStyle cellStyle = cell.getCellStyle();
short colorIdx=cellStyle.getFillForegroundColor();

HSSFWorkbook workbook = (HSSFWorkbook)myWorkBook;
HSSFPalette palette = workbook.getCustomPalette();
HSSFColor color = palette.getColor(colorIdx);

short[] triplet = color.getTriplet();
System.out.println("Now Color :"+triplet[0]+"-"+triplet[1]+"-"+triplet[2]);
}
}
Workbook myWorkBook=WorkbookFactory.create(新文件(文件路径));
工作表mySheet=myWorkBook.getSheetAt(0);
行currentRow=null;
迭代器rowIterator=mySheet.Iterator();
while(roweiterator.hasNext())
{
currentRow=(Row)rowIterator.next();
totalColumns=currentRow.getPhysicalNumberOfCells();
for(int column=0;column
在上面的代码中,我试图获得RGB颜色。问题是一些单元格颜色没有背景(没有填充),但是
color.getTriplet()返回0,0,0,即黑色背景色。如何区分和获取原始背景色。

Excel
单元格填充是模式填充。填充前景色是图案的颜色,填充背景色是图案后面的颜色

因此,只有在有填充图案的情况下,颜色才有意义,否则就没有意义。因此,通过获取填充图案而不是颜色来确定单元格是否填充

如果没有,则单元格已填充

在当前的
apache poi
版本中,您将执行以下操作:

...
CellStyle cellStyle = cell.getCellStyle();
FillPatternType patternType = cellStyle.getFillPattern();
if (patternType  != FillPatternType.NO_FILL) {
 short colorIdx = cellStyle.getFillForegroundColor();
 ...
在古代的
ApachePOI3.9
中,
CellStyle.getFillPattern
返回一个
short
。因此,必须:

...
CellStyle cellStyle = cell.getCellStyle();
short patternType = cellStyle.getFillPattern();
if (patternType  != 0) {
 short colorIdx = cellStyle.getFillForegroundColor();
 ...