简体   繁体   English

HSSFColor,无背景单元格返回错误的背景色

[英]HSSFColor with no background cell returning wrong background color

I am using apache poi 3.9 in my project. 我在我的项目中使用apache poi 3.9。 I am trying to read HSSF object excel cell and from that I am trying to get the background color 我正在尝试读取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]);
}
}

In above code I am trying to get RGB color.In that problem is some cell color have no background (NO Fill) but the color.getTriplet() is returning 0,0,0 ie black background color.How to differentiate and get the original background color. 在上面的代码中,我试图获取RGB颜色。在那个问题中,有些单元格颜色没有背景(没有填充),但是color.getTriplet()返回0,0,0,即黑色背景色。原始背景色。

Excel cell fills are pattern fills. Excel单元格填充是图案填充。 The fill foreground color is the color of the pattern and the fill background color is the color behind the pattern. 填充前景色是图案的颜色,填充背景色是图案后面的颜色。

So only if there is a fill pattern at all, the colors are meaningful, else not. 因此,仅当完全有填充图案时,颜色才有意义,否则就没有意义。 So do determining whether a cell is filled by getting the fill pattern and not by the color. 因此,通过获取填充图案而不是颜色来确定是否填充了单元格。

Do CellStyle.getFillPattern and then only if FillPatternType is not FillPatternType.NO_FILL , then the cell is filled. 执行CellStyle.getFillPattern ,然后仅当FillPatternType不是FillPatternType.NO_FILL时 ,才填充单元格。

In current apache poi versions you will do: 在当前的apache poi版本中,您将执行以下操作:

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

In the ancient apache poi 3.9 the CellStyle.getFillPattern returns a short . 在古代apache poi 3.9CellStyle.getFillPattern返回short So it would must be: 因此它必须是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM