使用Open XML 2.0在Excel中获取单元格背景颜色

我正在尝试在Excel电子表格中获取单元格的背景颜色。 我正在使用Open XML 2.0 SDK,我可以打开* .xlsx文件并获取单元格值。 我的代码获取背景颜色如下:

public BackgroundColor GetCellBackColor(Cell theCell, SpreadsheetDocument document) { BackgroundColor backGroundColor = null; WorkbookStylesPart styles = SpreadsheetReader.GetWorkbookStyles(document); int cellStyleIndex = (int)theCell.StyleIndex.Value; CellFormat cellFormat = (CellFormat)styles.Stylesheet.CellFormats.ChildElements[cellStyleIndex]; Fill fill = (Fill)styles.Stylesheet.Fills.ChildElements[(int)cellFormat.FillId.Value]; backGroundColor = fill.PatternFill.BackgroundColor; return backGroundColor; } 

我的问题在于, PatternFill.BackgroundColor只返回一个自然数,我认为它是样式的id。 我的问题是,那行代码

 DocumentFormat.OpenXml.Spreadsheet.Color c = (DocumentFormat.OpenXml.Spreadsheet.Color)styles.Stylesheet.Colors.ChildElements[Int32.Parse(backGroundColor.InnerText)]; 

返回一个错误,因为Stylesheet.Colorsnull ……也许是因为我在excel中使用了“内置”颜色 – 而不是自定义的颜色?!

任何想法如何能从“backGroundColor-Value”“计算”真实的颜色数?

Excel电子表格中单元格的填充图案由两种颜色组成:背景颜色和前景颜色。 前景色这个词在这里有一点点误导。 它不是字体的颜色,而是图案填充的前景色。

例如,如果使用纯色填充单元格的背景,则将单元格的相关PatternFill对象的ForegroundColor属性设置为select的纯色值,其中BackgroundColor对象设置为系统前景色。 PatternFill对象的PatternFill属性设置为PatternValues.Solid

因此,要获取单元格背景的颜色值(实心填充),必须分析相关的PatternFill对象的ForegroundColor属性。 您必须确定实例表示的“颜色types”:

  1. 自动颜色和系统相关的颜色
  2. 索引的颜色。
  3. ARGB颜色(阿尔法,红色,绿色和蓝色)
  4. 基于主题的颜色。
  5. 应用于颜色的色调值。

有关不同“颜色types”的更多信息,请参阅以下链接 。

请注意, ForegroundColorBackgroundColor类的InnerText属性的含义取决于颜色types。 例如,如果使用基于主题的颜色,则将InnerText属性设置为ColorScheme集合中的索引。

以下示例打印电子表格文档中所有单元格的所有背景颜色信息:

 public static PatternFill GetCellPatternFill(Cell theCell, SpreadsheetDocument document) { WorkbookStylesPart styles = SpreadsheetReader.GetWorkbookStyles(document); int cellStyleIndex; if (theCell.StyleIndex == null) // I think (from testing) if the StyleIndex is null { // then this means use cell style index 0. cellStyleIndex = 0; // However I did not found it in the open xml } // specification. else { cellStyleIndex = (int)theCell.StyleIndex.Value; } CellFormat cellFormat = (CellFormat)styles.Stylesheet.CellFormats.ChildElements[cellStyleIndex]; Fill fill = (Fill)styles.Stylesheet.Fills.ChildElements[(int)cellFormat.FillId.Value]; return fill.PatternFill; } private static void PrintColorType(SpreadsheetDocument sd, DocumentFormat.OpenXml.Spreadsheet.ColorType ct) { if (ct.Auto != null) { Console.Out.WriteLine("System auto color"); } if (ct.Rgb != null) { Console.Out.WriteLine("RGB value -> {0}", ct.Rgb.Value); } if (ct.Indexed != null) { Console.Out.WriteLine("Indexed color -> {0}", ct.Indexed.Value); //IndexedColors ic = (IndexedColors)styles.Stylesheet.Colors.IndexedColors.ChildElements[(int)bgc.Indexed.Value]; } if (ct.Theme != null) { Console.Out.WriteLine("Theme -> {0}", ct.Theme.Value); Color2Type c2t = (Color2Type)sd.WorkbookPart.ThemePart.Theme.ThemeElements.ColorScheme.ChildElements[(int)ct.Theme.Value]; Console.Out.WriteLine("RGB color model hex -> {0}", c2t.RgbColorModelHex.Val); } if (ct.Tint != null) { Console.Out.WriteLine("Tint value -> {0}", ct.Tint.Value); } } static void ReadAllBackgroundColors() { using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open("c:\\temp\\bgcolor.xlsx", false)) { WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; foreach(WorksheetPart worksheetPart in workbookPart.WorksheetParts) { SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First(); foreach (Row r in sheetData.Elements<Row>()) { foreach (Cell c in r.Elements<Cell>()) { Console.Out.WriteLine("----------------"); PatternFill pf = GetCellPatternFill(c, spreadsheetDocument); Console.Out.WriteLine("Pattern fill type -> {0}", pf.PatternType.Value); if (pf.PatternType == PatternValues.None) { Console.Out.WriteLine("No fill color specified"); continue; } Console.Out.WriteLine("Summary foreground color:"); PrintColorType(spreadsheetDocument, pf.ForegroundColor); Console.Out.WriteLine("Summary background color:"); PrintColorType(spreadsheetDocument, pf.BackgroundColor); } } } } } static void Main(string[] args) { ReadAllBackgroundColors(); }