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.
There are static color classes provided by the HSSFCell class, listed here:
http://poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html
If you want to create your own custom colors, you will need to create and modify a custom palette. Apache provides a very clear guide to this as well:
http://poi.apache.org/spreadsheet/quick-guide.html#CustomColors
To get the color : The short value returned by the getFillBackgroundColor is the Excel index of the color. You can get the color corresponding to the index in the HSSFColor HashTable, using the last code RMorrisey indicated.
To set a color : You create a custom palette, and change the color at a given index. Then, you apply the color to the style.
Regards
Guillaume
The following is for XSSF and is in Scala but it does show exactly how to get the colour from the object model. I wanted to instantiate a java.awt.Color object from the actual rgb values (which is useful partly because my debugger displays for me the actual colour of the object when I stop at breakpoints, and partly because this is for export to systems that have nothing to do with Excel). I'm ignoring the colour's alpha value and my Scala may be a bit naive. I'd suggest that if this doesn't work for you, you should set a break-point and examine the result of closely related method calls such as getFillBackgroundColorColor()
To get the background color of the specific cell in HEX, use this:
Notice the word
Color
is used twiceYou would do something like:
I believe there is a limited number of styles for a given workbook; you will want to reuse the same style object where possible.
[Edit: Sorry, that would be to set the color on a cell. To get the color, use like:
]
[Edit 2: Looking at the custom color information craig posted, maybe you can try:
]