Free Support Forum - aspose.com

How to Change a Cell's Color on the Fly

Hello,

I have the following method that I would like to call to change the Color of the referenced cell. (See bottom of method).

The logic is to use a Case statment to determine which Color to use. I will eventually have up to TEN Colors to pick from.

When I run against the method below, the referenced cells color displays as BLACK not the nice hue of Yellow I wanted for example.

I appreciate any ideas. In short, I want to change the Color on the fly so, to speak.

Thanks

private void setCertNameStyle(Worksheet s, Workbook b) {

Font font = null;

Color cCertColor = null;

gColorPicker++;

String colorPickerName = String.valueOf(gColorPicker);

//Adding a new Style to the styles collection of the Excel object

Style style = b.createStyle();

style.setName("certStyle"+colorPickerName);

switch (gColorPicker) {

case 1: cCertColor = new Color((short)255,(short)204,(short)153); break;

case 2: cCertColor = new Color((short)255,(short)255,(short)153); break;

default: cCertColor = new Color((short)255,(short)204,(short)153); break;

}

// set selected cell color

style.setColor(cCertColor);

// set text to center

style.setHAlignment(TextAlignmentType.CENTER);

// set font

font = style.getFont();

font.setBold(true);

font.setSize(11);

style.setFont(font);

// set the final style for the cell

s.getCells().getCell(gRow,0).setStyle(style);

}

Hi,

Kindly try our latest version v2.3.0: http://www.aspose.com/community/files/72/java-components/aspose.cells-for-java/entry243913.aspx, we have tested and your code and it works fine with it.

Well, MS Excel (97-2003) standard color palette has only 56 colors, and
if you
need to apply custom colors for different objects, you need to add
these colors to the MS Excel standard color palette first. It is to
be noted here, if you save the file in MS Excel 2007 format e.g XLSX,
then you don’t need to do that
.



See the topic for your reference: http://www.aspose.com/documentation/java-components/aspose.cells-for-java/colors-and-palette.html


Thank you.