locked
How to convert excel cell color to dotnet color RRS feed

  • Question

  • i am reading excel file by EPPlus and many cell has violet color. now how could i detect cell has violet color. so tell me how to convert excel color to dotnet cell color?

    i found this url but not sure does it work ?

    https://stackoverflow.com/a/52615585

    thanks

    Monday, May 11, 2020 6:47 AM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    The method in the link can be used to determine whether a cell has a certain background color.

                ExcelPackage.LicenseContext = LicenseContext.Commercial;
    
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                using (var package = new ExcelPackage(new FileInfo(@"D:\test\excel\test.xlsx")))
                {
    
                    ExcelWorkbook workbook = package.Workbook;
    
                    ExcelWorksheet currentWorksheet = workbook.Worksheets.First();
    
                    ExcelRange theCell = currentWorksheet.Cells[11, 6];
                    var re = theCell.Style.Fill.BackgroundColor;
                    if (theCell.Style.Fill.BackgroundColor.Rgb == Color.Yellow.A.ToString("X2") + Color.Yellow.R.ToString("X2") + Color.Yellow.G.ToString("X2") + Color.Yellow.B.ToString("X2"))
                    {
                       
    
                    }

    If you need to convert it to System.Drawing.Color commonly used in C#, you can use the following code.

     string re = theCell.Style.Fill.BackgroundColor.Rgb;
     int argb = Int32.Parse(re.Replace("#", ""), NumberStyles.HexNumber);
     Color clr = Color.FromArgb(argb);

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Monday, May 11, 2020 10:12 AM
    Monday, May 11, 2020 9:33 AM