How can I assign a color to a font in EPPlus?
Asked Answered
R

2

32

I can set the background color of a cell or range of cells like so:

rowRngprogramParamsRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
rowRngprogramParamsRange.Style.Fill.BackgroundColor.SetColor(Color.DarkRed);

I have not been able to set the font color, though. I tried this:

rowRngprogramParamsRange.Style.Font.Color = Color.Red;

...which failed to compile with two err msgs: the first, that I cannot assign System.Drawing.Color to OfficeOpenXml.Style.ExcelColor, and the second that the property is readonly anyway.

Just for grin and bear its, I tried casting the value:

rowRngprogramParamsRange.Style.Font.Color = (OfficeOpenXml.Style.ExcelColor)Color.Red;

...and I now get, "Cannot convert type 'System.Drawing.Color' to 'OfficeOpenXml.Style.ExcelColor'"

Most everything in EPPlus is pretty easy, certainly easier than Excel Interop, but this one has me baffled. How does one assign a color to a font for a range in EPPlus?

Rosetta answered 11/8, 2016 at 22:39 Comment(0)
P
60

It's safe to assume Style.Fill.BackgroundColor and Style.Font.Color are both of type ExcelColor, so just use the same SetColor() method you used to set the background color.

rowRngprogramParamsRange.Style.Font.Color.SetColor(Color.Red);
Pentane answered 11/8, 2016 at 22:53 Comment(0)
M
1

In addition, I'd say that if you want the exact excel color, the best way I've found is by copying a screenshot of an excel spreadsheet displaying the desired color into ms paint, to get its hex code from there. after that, you just add the so obtained the rgb code this way.

rowRngprogramParamsRange.Style.Font.Color.SetColor(0, 244, 176, 132)

The first parameter can stay at 0. The current color is orange accent 2 lighter 40. Light salmon is close to that, but not quite..

Monogenetic answered 5/3, 2021 at 15:43 Comment(2)
How do you get the RGB from Paint?Rosetta
there is a tool that looks like a dropper, allowing you to copy a color. You copy the desired color, then right to the colors, there is button for modifying the color. Bottom right of the popup will display the rgb code.Monogenetic

© 2022 - 2024 — McMap. All rights reserved.