Free Support Forum -

Trouble with setting ForegroundColor using ColorTranslator.FromHTML


I'm having trouble with colors using the ColorTranslator.FromHTML method.

Whenever I set a Style.ForegroundColor using this method, I have to use the ChangePalette method for the color to take effect.

Also, I have to use a different index on the ChangePalette for each color change.

For the first cell i have to set that Palette and foreground color.

excel.ChangePalette(ColorTranslator.FromHtml("#E9E9C9"), 55);

excel.Worksheets[0].Cells[4,0].Style.ForegroundColor = ColorTranslator.FromHtml("#E9E9C9");

Then to change the color for the next cell:

excel.ChangePalette(ColorTranslator.FromHtml("#f9f9ee"), 54);

excel.Worksheets[0].Cells[4,1].Style.ForegroundColor = ColorTranslator.FromHtml("#f9f9ee");

Is there any way do get around having to use ChangePalette method for every color change?



Hi Faiz,

MS Excel can only contain 56 different colors in an Excel.

You can try to call Excel.GetMatchingColor to get a matching color in the palette.