Making new colors in JExcelApi
Asked Answered
O

3

7

I'm using JExcelApi for generating XLS files. From jxl.format.Colour, I see how to get any of the colors in the "standard Excel colour palette", but not how to create a new color (say, given its RGB).

But in Excel itself, I can pick any color at all.

Am I just missing it? Is there a way in JExcelApi to select an arbitrary color? I'm using a simple find-the-closest-standard-color method right now, which is OK but not great.

Orpha answered 2/12, 2009 at 18:45 Comment(0)
B
3

Excel versions before 2007 have a standard palette, and given that the API you are using does not support the 2007 format, you may be stuck with that. The reason you can choose any colour you want is probably because you are using a new version of Excel.

See this information on the Microsoft site.

I don't see how you could override the standard colour palette in the API you are using, but in Apache POI (which also lets you write Excel files) you can: see this link. Basically, what you need to do there is: assign certain standard colours (green, etc) to your cells; then override these colours with whatever custom colour you need.

Breton answered 22/12, 2009 at 11:23 Comment(2)
This is a great start, thanks. I'll check out the Apache POI source to see if changing the standard palette looks difficult.Orpha
Unfortunately this isn't the answer but I'm no longer allowed to change my mind for some reason -- if you're looking for the actual solution, see "Damien B"'s answer below (setColourRGB).Orpha
O
16

The way to override a palette index in JExcel API is to use the method [setColourRGB][1] on the writable workbook. For instance:

myWorkbook.setColourRGB(Colour.LIGHT_TURQUOISE2, 14, 67, 89);

if you want to change the color value in the palette entry where there is by default the second light turquoise. Or, more easily in some cases, directly with the palette index:

myWorkbook.setColourRGB(Colour.getInternalColour(myPaletteIdx), 14, 67, 89);

A small update: only the indexes from 8 to 64 can be customized according to a comment inside the source code of jxl.biff.PaletteRecord.

[1]: http://jexcelapi.sourceforge.net/resources/javadocs/current/docs/jxl/write/WritableWorkbook.html#setColourRGB(jxl.format.Colour, int, int, int)

Oeflein answered 7/1, 2010 at 21:0 Comment(2)
This looks perfect! Thanks! (Unfortunately S.O. won't let me mark this as the "correct" answer.)Orpha
You still could have upped the answer :-) I have added a note: if the requested index is strictly below 8, the call to setColourRGB is silently ignored.Oeflein
B
3

Excel versions before 2007 have a standard palette, and given that the API you are using does not support the 2007 format, you may be stuck with that. The reason you can choose any colour you want is probably because you are using a new version of Excel.

See this information on the Microsoft site.

I don't see how you could override the standard colour palette in the API you are using, but in Apache POI (which also lets you write Excel files) you can: see this link. Basically, what you need to do there is: assign certain standard colours (green, etc) to your cells; then override these colours with whatever custom colour you need.

Breton answered 22/12, 2009 at 11:23 Comment(2)
This is a great start, thanks. I'll check out the Apache POI source to see if changing the standard palette looks difficult.Orpha
Unfortunately this isn't the answer but I'm no longer allowed to change my mind for some reason -- if you're looking for the actual solution, see "Damien B"'s answer below (setColourRGB).Orpha
R
3
WritableCellFormat cellFormat = new WritableCellFormat();

Colour customColor = new Colour(10000, "1", 255, 0, 0){     
};

cellFormat.setBackground(customColor);  
writableCell.setCellFormat(cellFormat);
Revue answered 1/4, 2013 at 11:55 Comment(1)
I tried that, but I got an error: Error: java: Colour(int,java.lang.String,int,int,int) has protected access in jxl.format.ColourDonetsk

© 2022 - 2024 — McMap. All rights reserved.