简体   繁体   English

无法从.xlsx单元读取RGB数据

[英]Unable to read RGB data from .xlsx cell

I have a simple .xlsx file which has cells of different colors. 我有一个简单的.xlsx文件,其中包含不同颜色的单元格。 I need to get the colors and check if they are RED, YELLOW or GREEN. 我需要获取颜色并检查它们是红色,黄色还是绿色。

The excel file looks like this: excel文件如下所示:

在此处输入图片说明

For that I tried to write the code as following: 为此,我尝试编写如下代码:

// get Cell color for specificed cells
        CellStyle cellStyle = currentCell.getCellStyle();
        XSSFCellStyle xssfCellStyle = (XSSFCellStyle) cellStyle;
        XSSFColor color = xssfCellStyle.getFillBackgroundXSSFColor();

        // check for null - when there is no cell background color, it returns null
        if (null != color) {
            String rgb =  color.getCTColor().getDomNode().getAttributes().getNamedItem("rgb").getNodeValue();
        }

This code throws NullPointerException, because, here color.getCTColor().getDomNode().getAttributes().getNamedItem("rgb") == null . 此代码引发NullPointerException,因为此处color.getCTColor().getDomNode().getAttributes().getNamedItem("rgb") == null

Then I worked on another approach which is following: 然后,我研究了以下另一种方法:

// get Cell color for specificed cells
        CellStyle cellStyle = currentCell.getCellStyle();
        XSSFCellStyle xssfCellStyle = (XSSFCellStyle) cellStyle;
        XSSFColor color = xssfCellStyle.getFillBackgroundXSSFColor();
        if (color != null) {
            cellObject.setBackgroundColor(color.getARGBHex());
            System.out.println("row: " + rowIndex + "col: " + colIndex + "color : " + color.getARGBHex());
        }

But, in this approach the the value for color.getARGBHex() in each cell as null . 但是,在这种方法中,每个单元格中color.getARGBHex()的值都为null

Edit: I tried another simpler approach which is the following: 编辑:我尝试了另一种更简单的方法,如下所示:

//get cell colors
        short style = currentCell.getCellStyle().getFillBackgroundColor();
        System.out.println("row:"+rowIndex+",col:"+colIndex+" color: "+style);

This code returns the value for every cell: 64 此代码返回每个单元格的值: 64

Edit: As per Axel's suggestion, I tried this code: 编辑:根据Axel的建议,我尝试了以下代码:

short xssfColor = currentCell.getCellStyle().getFillForegroundColor();

But, this code returns 64 for white cells and 0 for non-white cells. 但是,此代码对于白细胞返回64,对于非白细胞返回0。

Can anyone please help in this regard? 任何人都可以在这方面提供帮助吗? Thanks in advance! 提前致谢!

Thanks Axel for the hint. 感谢Axel的提示。 Using his hint, I was able to solve this problem. 使用他的提示,我能够解决此问题。

I used this code for solving this problem. 我使用此代码解决了这个问题。

// get cell colors
        XSSFColor xssfColor = (XSSFColor) currentCell.getCellStyle().getFillForegroundColorColor();
        if (xssfColor != null)
            System.out.println("row:" + rowIndex + ",col:" + colIndex + " color: " + xssfColor.getARGBHex());

This clearly differentiates between all the colors. 这显然可以区分所有颜色。 getARGBHex() returns RGB components along with the Alpha value. getARGBHex()返回RGB分量以及Alpha值。 But, it is easy string operation to extract the RGB from the data 但是,从数据中提取RGB很容易进行字符串操作

For example: 例如:

Red : FFFF0000 (returned by getARGBMex() method, so RGB value is: FF0000). Red : FFFF0000 (由getARGBMex()方法返回,因此RGB值为:FF0000)。

Thanks again Axel for your help. 再次感谢Axel的帮助。

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

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