简体   繁体   English

使用EPPlus库检查Excel文件的背景色

[英]Checking the Background Color of Excel file using EPPlus library

I am using the Open Source EPPlus library which allows you to read Spreadsheet files like Excel. 我正在使用开源EPPlus库,该库允许您读取Excel等电子表格文件。 Now, I have an Excel file but I want to check the Background Color of a Cell before I get the value of a cell. 现在,我有一个Excel文件,但是我想在获取单元格的值之前检查单元格的Background Color However, I don't know what enum to use. 但是,我不知道使用什么enum Here is my example code below: 这是我的示例代码如下:

            using (var package = new ExcelPackage(excelFile))
            {
                ExcelWorkbook workbook = package.Workbook;

                ExcelWorksheet currentWorksheet = workbook.Worksheets.First();

                ExcelRange theCell = currentWorksheet.Cells[8, 1];
                if (theCell.Style.Fill.BackgroundColor == whatShouldBeTheEnumHere)
                {
                  String getValue = theCell.Value.ToString();
                }

            }

Any suggestions? 有什么建议么?

I got the answer to my own question :-) I found out that BackgroundColor has an RGB property and that's what I used to get the value of color that I want to test. 我得到了自己的问题的答案:-)我发现BackgroundColor具有RGB属性,这就是我用来获取要测试的颜色值的原因。 This is the code 这是代码

        using (var package = new ExcelPackage(excelFile))
        {
            ExcelWorkbook workbook = package.Workbook;

            ExcelWorksheet currentWorksheet = workbook.Worksheets.First();

            ExcelRange theCell = currentWorksheet.Cells[8, 1];
            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"))
            {
              String getValue = theCell.Value.ToString();
            }

        }

Or of course I could use a function to return the HexValue like 或者当然我可以使用一个函数来返回HexValue

   if (theCell.Style.Fill.BackgroundColor.Rgb == ColorHexValue(Color.Yellow))
      {
        String getValue = theCell.Value.ToString();
      }

And the the function to return the Hex Value: 以及返回十六进制值的function

   private String ColorHexValue(System.Drawing.Color C)
    {
        return C.A.ToString("X2") + C.R.ToString("X2") + C.G.ToString("X2") + C.B.ToString("X2");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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