Foreground/Background color problem

Simple problem that is mostly likely easily solved - I have the following in my code that has no effect

cells[1, 2].Style.ForegroundColor = System.Drawing.Color.Red;

cells[curRow, curCol].Style.BackgroundColor = System.Drawing.Color.Blue;

I'm using latest version of Aspose.Cells. Spreadsheets is being saved in Excel2003 format and being loaded into Excel 2007. Any suggestions as to what I'm doing wrong are greatly appreciated.

Hi,

Thanks for considering Aspose.

When you use Style.ForegroundColor / Style.BackgroundColor properties, you have to set the Pattern property as well. Please read the description / explanation below for your reference.

Explanation:

In MS Excel there are three types of color normally applied to a cell. i.e., ForegroundColor (refers to fill color or cell shading color), BackgroundColor (refers to background color, occasionally used) and Font Color (refers to font text color of a cell).

When you want to set a solid fill color (shading color) of a cell you may use ForgroundColor property. The following code paints a yellow background color to the cell (You don't need to confuse with BackgroundColor property with it. You may use ForegroundColor property to set you desired back color of the cell. This code makes a yellow solid background) .

cell.Style.ForegroundColor = Color.Yellow;

cell.Style.Pattern = BackgroundType.Solid;

Well, BackgoundColor normally applied when you set pattern's BackgroundType (enum) to other than .None or other than .Solid. e.g.,

worksheet.Cells["A2"].Style.ForegroundColor=Color.Blue;

worksheet.Cells["A2"].Style.BackgroundColor=Color.Yellow;

worksheet.Cells["A2"].Style.Pattern=BackgroundType.VerticalStripe;

Here, the background fill color will be yellow with Blue Stripes in front of it.

For Font text color you may use:

cell.Style.Font.Color = Color.Red;

I write a simple example for you:

Workbook wb= new Workbook();

// If the color is not in the standard color palette you have to add it into the

//palette. For your info, standard color palette has 56 colors (0-55) indexed

wb.ChangePalette(System.Drawing.Color.SkyBlue, 55);

Cells cells = wb.Worksheets[0].Cells;

Cell cell = cells[0,0];

cell.PutValue("Testing");

int index = wb.Styles.Add();

Style style = wb.Styles[index];

//For Cell's filling color

style.ForegroundColor = System.Drawing.Color.SkyBlue;

style.Pattern = BackgroundType.Solid;

// For text color

style.Font.Color = Color.White;

style.HorizontalAlignment = TextAlignmentType.Center;

cell.Style = style;

wb.Save("d:\\test\\testclr.xls");

Hoping you got it!

Thank you.

Hi ,

I want to set the background color of the row or cell with this value #BDEDFF using java.

How set this(#BDEDFF ) value for color in style or directly in cell of the Excel using java.

Regards

Janakiraman

Hi,<?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” />

Thank you for considering Aspose.

Well, you may need to change the default color palette to use the custom color which is not the part of Excel 2003 default color palette. If you set a color which is not in the palette, it will not take effect. Please see the following documentation links for Default Color Palette and how to add the custom colors in the Palette,

Also, please see the following sample code for applying your mentioned color to a row,

Sample Code:

//Instantiating a Workbook object

Workbook workbook =  **new**  Workbook();

//Adding custom color to the palette at 55th index

Color color =  **new**  Color();

color.setRed(189);

color.setBlue(237);

color.setGreen(255); 

workbook.getPalette().setColor(55, color);

//Obtaining the reference of the newly added worksheet by passing its sheet index

Worksheet worksheet = workbook.getWorksheets().getSheet(0);

Row row = worksheet.getCells().getRow(0);

//Setting the custom color to the font

Style style = row.getStyle();

//Setting the foreground color to yellow

style.setColor(color);

//Setting the background pattern to vertical stripe

style.setPatternStyle(PatternType. *SOLID* );

//Saving the modified style to the "A1" cell.

row.setStyle(style);

//Saving the Excel file

workbook.save("C:\\output.xls",FileFormatType. *DEFAULT* );

Thank You & Best Regards,

Hi aslam ,

Am reading the BG color (#BDEDFF ) of a table from HTML and creating the table format

in Excel .I want the same bgcolor #BDEDFF applied to the Cells in Excel too

Regards

Janakiraman

Hi,

Thank you for considering Aspose.

Well, you can assign the custom color you are getting from your HTML file to workbook.getPalette().setColor(55, color); and then use same color in the style.setColor(color); method. This way the color which you have extracted from HTML will be applied as the color in Excel.

Thank You & Best Regards,

Hi aslam ,

The Bgcolor of each row of the table will changes alternatively so i used the below code

Style cellBGColor1 = workbook.createStyle();

cellBGColor1.getPatternColor().setBlue(b);

cellBGColor1.getPatternColor().setGreen(g);

cellBGColor1.getPatternColor().setRed(r);

is it possible to set the color to workbook workbook.getPalette().setColor(55, color);

multiple colors to it dynamically.

Regards

Janakiraman

Hi,

Thank you for considering Aspose.

Yes, it is possible to add multiple custom colors in your palette. The first parameter of the method workbook.getPalette().setColor(colorIndex, color) is the index of the color in the palette which you are replacing with your own custom color. So, you can use index 0 to 55 (means 55 custom colors) dynamically as per your requirement.

Please do let us know if you need any further assistance in this regard.

Thank You & Best Regards,

Hi,

Thanks for your reply .

I will try it out with different index.

Janakiraman