Setting Foregroundcolor in cells.cell

Hi,
I am trying to set the foreground color using the cell.style.Foregroundcolor property. But for some reason it is defaulting to black and all the text in the cell gets hidden under that black color.

I am trying to set the cell fill color, am i on the right path?

Hi,

Thanks for considering Aspose.

Well, the color your are applying might not be present in the Standard Color Palette, so you have to add it first into the palette before setting it as a fill color of the cell. Since the Excel color palette has only 56 colors (0-55 indexed) on it, so if a color is not there, you will add the color to the palette replacing any existing color on a specified index position.

Here is a sample code for your need:

//Instantiating a Workbook object

Workbook workbook = new Workbook();

// Add sky blue color to the palette.

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

//Obtaining the first worksheet by passing its sheet index

Worksheet worksheet = workbook.Worksheets[0];

//Now setting the foreground color of the "A1" cell to sky blue

worksheet.Cells["A1"].Style.ForegroundColor = Color.SkyBlue;

//Setting the background pattern of the "A1" cell to solid

worksheet.Cells["A1"].Style.Pattern = BackgroundType.Solid;

//Saving the Excel file

workbook.Save("d:\\test\\fillcolors.xls",FileFormatType.Default);

Thank you.

Hi amjad,
thanks for the response.

I followed the procedure to add the extra color to the palette, but now instead of the standard black, the color is defaulting to white - i am assuming that when i save the file to excel format, the color palette in excel still has those default 56 colors and not the new one that i have added to the workbook.
I am trying to add this color as an example (ffc6c7de ARGB = (255,198,199,222))

Please let me know if there is any extra step to make sure such colors also show up in the saved excel file.

regards,
Jaideep.

Hi Jaideep,

Well, the color (ARGB = (255,198,199,222)) is not present in the standard color palette, so you have to add it to the palette. And for your info, this color is whitish gray to some extent not completely white. And here is my code which works fine here, Kindly check the output xls file (attached) and also check the picture (attached) in which I have tried to show you and locate the color in the color palette:

Workbook workbook = new Workbook();

workbook.ChangePalette(Color.FromArgb(255,198,199,222),55);

Worksheet worksheet = workbook.Worksheets[0];

Cells cells = worksheet.Cells;

Cell cell = cells["A1"];

cell.Style.ForegroundColor = Color.FromArgb(255,198,199,222);

cell.Style.Pattern = BackgroundType.Solid;

workbook.Save("d:\\test\\cellclrs.xls");

If you still find problems, could you post your test code with output file here, so that we may check and resolve your issue soon.

Thank you.

Amjad,
thanks for the reply again.

When i am trying to build my excel document, i come across a lot of colors that might not be in the palette - is there a method or any other handle which i can use to compare a color to the existing palette to see if its already in there ?
That way i can compare and only add the ones that are not in there already.

regards
Jaideep.

Hi,

Yes, please check the color list with the RGB values of the colors (56 colors) present on the standard excel color palette.

Aspose.Cells API Reference pages

So you may try to compare a color and check whether it is there on the color palette before adding it to the color palette.

Thank you.

I’ve come across this problem too. I was trying to set the background color to be Color.DarkBlue and the Font Color to be white. I found that to achive what I wanted, I actually had to set the ForegroundColor to Dark Blue, not the Background Color, which I found unintuitive to say the least. Not sure if this is a known issue.

Cheers
Paul

… so to clarify

If you want black text, you need to set Font.Color rather than ForegroundColor.

In my experience anyway. and if you’re talking about a drawing color, that’s probably different

Hope this helps

Cheers
Paul

Hi Paul,

Thanks for considering Aspose.

Well, I try to explain you:

Well, 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).

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.

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 Amjad, OK that makes sense now. Thanks for the reply. Also I now see that my problem is not related to Jaideep’s original problem at all!!

cheers
Paul

Hi Paul,

Yes, you are right. But had it not be better, if you could post your query in a separate thread.

Keep Smiling!

Thank you.

Paul,
Actually i found that a little confusing too - because logically background color would indicate fill color - but it is an excel thing per se :slight_smile:

amjad - thanks for your response, i am in the right direction now.
I also stumbled across the workbook.isColorInPalette() method which checks to see if a color is already present in the palette.