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.
Apache POI provides three methods for changing the background color. In the CellStyle class, we can use the setFillForegroundColor, setFillPattern, and setFillBackgroundColor methods for this purpose. A list of colors is defined in the IndexedColors class. Similarly, a list of patterns is defined in FillPatternType.
Code ExplanationXSSFCellStyle style=workbook. createCellStyle(); Then style the background and foreground by setFillBackgroundColor and setFillPattern, then give the cell value and cell style.
Select the cell or range of cells you want to format. Click Home > Format Cells dialog launcher, or press Ctrl+Shift+F. On the Fill tab, under Background Color, pick the color you want.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With