Set custom BackgroundColor of a Excel sheet cell using epplus c#

Hakuna Matata picture Hakuna Matata · Jun 8, 2013 · Viewed 75.6k times · Source

The problem:

I am using EEPlus.

I am stuck at applying a hex color code, e.g. #B7DEE8, for a cell in my Excel sheet.

I got the following (working) code:

ws.Cells["A1:B1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(Color.Gray);

But I need something like the following:

ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor("#B7DEE8");

So my question is: is it possible to use hex color codes with EEPlus? If so, how can I do that?

Answer

Yograj Gupta picture Yograj Gupta · Jun 8, 2013

Try this

Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#B7DEE8");
ws.Cells["A1:B1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(colFromHex);