How do I get the (Java Apache POI HSSF) Background

2020-02-09 09:52发布

I have an existing excel spreadsheet, which I am accesssing and reading values from, I am using Apache POI HSSF.

It is initialised like this:

HSSFSheet sheet;
FileInputStream fis = new FileInputStream(this.file);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook wb = new HSSFWorkbook(fs);
this.sheet = wb.getSheet(exsheet);

I am iterating over all the cells that exist in the sheet, which makes a cell object:

HSSFCell cell = (HSSFCell) cells.next();

Please can someone familiar with the framework explain how to create an (HSSFColor) object to represent the backround color of each cell in the sheet.

Many thanks

EDIT, UPDATE

To be clear what I want to know is: how do I create/get an HSSFColor object for the background color of an existing cell?

cell.getCellStyle().getFillBackgroundColor(); 

This code only returns a short number, not an HSSFColor object. Thanks for the answers so far.

8条回答
唯我独甜
2楼-- · 2020-02-09 10:49

The backgroundcolor information of XSSFCellStyle can get from the method:

XSSFCellStyle.getFillForegroundXSSFColor().getCTColor()

You can print it out and you will see it's structure.

查看更多
狗以群分
3楼-- · 2020-02-09 10:57

For XSSF reading xlsx file (tried the HSSF as well) ,after struggle for a while, i just found getFillBackgroundXSSFColor() method actually returned the "Pattern Color" in the Fill tab of "Format Cells" in Excel, not the so-called "Background" color in that tab. I am not sure if this expected.

See my below screenshot. The returned RGB is actually FF0000 ,i.e. RED.

        XSSFColor backgroundColor = cell.getCellStyle().
            getFillBackgroundXSSFColor();
    System.out.println("backgroundColor is "+backgroundColor.getARGBHex());

    Output: FFFF0000 //the first FF should be ignored.

So right now, I do not have a way for this case and just request user to fill the "Pattern Color" as well.

enter image description here

查看更多
登录 后发表回答