dutchman79
dutchman79

Reputation: 483

Duplicating Excel colors with Apache Poi

In my Excel File a macro looks for certain cell values and then changes the fillcolor.

I have all the RGB values for these colors and want to set exactly these colors when I write data with Apache POI in a file.

How can I do this ?

Upvotes: 1

Views: 912

Answers (2)

Envil
Envil

Reputation: 2727

I had faced a problem may be similar to your problem before. First look at this example for sure that you know the method to fill color for a cell:
http://poi.apache.org/spreadsheet/quick-guide.html#CustomColors
It's seem fine, but I figured out a common error that it may easily be taken. If you try to set the cell value and style in a loop, so the style declaration must be fresh every round of loop. That mean you have to reinitialize your style variable inside the loop. It's somehow like you put this declaration inside your loop:

for(i=0;i<rowsize;i++){
    //
    //I suppose that we have an instance named row to working on.
    //
    XSSFCell cell = row.getCell(i);
    XSSFCellStyle style1 = wb.createCellStyle(); //create a fresh instance
    cell.setCellValue("custom XSSF colors");    //Set the cell value

    //This two line will setup the style of your cell with your needs
    style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128)));
    style1.setFillPattern(CellStyle.SOLID_FOREGROUND);

    //Finally apply your style
    cell.setCellStyle(style1);
}

Upvotes: 3

polypiel
polypiel

Reputation: 2341

The only way I know to add a custom color in POI is changing a default color:

Workbook wb = ...;    
wb.getCustomPalette().setColorAtIndex(HSSFColor.LIGHT_ORANGE.index, (byte) 255, (byte) 171, (byte) 115);

Upvotes: 0

Related Questions