简体   繁体   中英

How to convert System.Drawing.Color to ClosedXML.Excel.XLColor in C#

I am working with ClosedXML and the background color of a specific cell could be set like the following code.

using (var workbook = new XLWorkbook())
{
    var worksheet = workbook.Worksheets.Add("Sample Sheet");
    worksheet.Cell("A1").Value = "Hello World!";
    worksheet.Cell("A1").Style.Fill.BackgroundColor = ClosedXML.Excel.XLColor.AliceBlue;
    //  Fill background color as AliceBlue.
    workbook.SaveAs("HelloWorld.xlsx");
}

My question is:

Is there appropriate method to convert System.Drawing.Color to ClosedXML.Excel.XLColor?

Any comments or suggestions are welcome.

I've never used ClosedXml but the fine manual shows many ways an XLColor can be created. I picked on the first when writing this answer initially:

var c = Color.Red;
var xlc = XLColor.FromArgb(c.A, c.R, c,G, c.B);

@FrancoisBotha helpfully pointed out that there is an overload that takes the color directly:

var c = Color.Red;
var xlc = XLColor.FromColor(c);

You can see the other ways in the manual..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM