简体   繁体   English

如何获取单元格的颜色?

[英]How can I retrieve the cell's color?

I want to get the cell color with epplus. 我想使用epplus获得单元格颜色。 In an Excel cell I have coloured text. 在Excel单元格中,我给文本加上了彩色。 This code not return the cell color. 此代码不返回单元格颜色。 I cannot understand why. 我不明白为什么。 I want to get all the colors of the cell and then add it to a Dictionary with text. 我想获取单元格的所有颜色,然后将其添加到带有文本的Dictionary

FileInfo file = new FileInfo("K:\\1.xlsx");
var package = new ExcelPackage(file);

ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
var start = worksheet.Dimension.Start;
var end = worksheet.Dimension.End;
    for (int row = start.Row; row <= end.Row; row++)
        { 
              for (int col = start.Column; col <= end.Column; col++)
              { 
                 var color = worksheet.Cells[row, col].Style.Font.Color;

              }
         }

在此处输入图片说明

There are two ways text can become colored in .xlsx files. .xlsx文件中的文本可以通过两种方式着色。

  • There can be a style applied to the cell (the approach you are trying to use above) 可以将一种样式应用于该单元格(上面您尝试使用的方法)
  • The cell can contain "Rich Text" content which is itself colored 单元格可以包含本身是彩色的“ Rich Text”内容

If the cell contains text of multiple colors then it is the latter of these options and we must therefore use the .RichText property to access the colors. 如果单元格包含多种颜色的文本,则这是这些选项中的后者,因此,我们必须使用.RichText属性来访问颜色。 We can also test to determine if a cell contains Rich text with the .IsRichText property. 我们还可以测试以确定单元格是否包含具有.IsRichText属性的.IsRichText

As an example take this workbook: 以此工作簿为例:

在此处输入图片说明

Then when running this code: 然后在运行此代码时:

    static void Main(string[] args)
    {
        Dictionary<string, Color> dictionaryTextToColour = new Dictionary<string, Color>();

        var fInfo = new FileInfo(@"C:\Temp\sample.xlsx");
        using (var package = new ExcelPackage(fInfo))
        {

            var a1 = package.Workbook.Worksheets.First().Cells[1, 1];

            if (a1.IsRichText)
            {
                var richText = a1.RichText;
                dictionaryTextToColour = richText 
                    .ToDictionary(rt => rt.Text, rt => rt.Color); //NOT recommended to use a dictionary here
            }
        }

        foreach (var substring in dictionaryTextToColour.Keys)
        {
            Console.WriteLine(substring + ": " + dictionaryTextToColour[substring]);
        }
    }

we see this output: 我们看到以下输出:

Some: Color [Empty]
    TextW: Color [A=255, R=0, G=176, B=80]
    ithVarious: Color [A=255, R=0, G=32, B=96]
    Colour: Color [A=255, R=255, G=0, B=0]
    s: Color [Empty]

The .RichText property of type ExcelRichTextCollection which implements IEnumerable<ExcelRichText> so we could just as well have iterated over each text segment with something like: 类型为ExcelRichTextCollection.RichText属性实现了IEnumerable<ExcelRichText>因此我们同样可以对每个文本段进行如下迭代:

foreach (var rtSubString in a1.RichText)
{
    //do something with the string segment rtSubString
}

NB I have added the colors to a dictionary keyed with the text as that is what was asked for in the original question but I don't think that is the best data structure as the same substring could be repeated with different colors 注意:我已将颜色添加到以文本为键的字典中,因为这是原始问题所要求的,但是我认为这不是最佳的数据结构,因为相同的子字符串可以用不同的颜色重复

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

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