使用Open XML 2.0在Excel中获取cell-backgroundcolor

bas*_*sti 6 c# excel .net-4.0 openxml-sdk

我试图在excel-spreadsheet中获取单元格的backgroundcolor.我正在使用Open XML 2.0 SDK,我可以打开*.xlsx文件并获取单元格值.我获取Background-Color的代码如下:

   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;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是,PatternFill.BackgroundColor这只返回一个自然数,我认为它是风格的id.我的问题是,那行代码

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

一个错误返回,因为Stylesheet.Colorsnull......也许是因为我使用了"中建"颜色在Excel中-而不是一个自定义的颜色?

我有什么想法可以"计算""backGroundColor-Value"中的真实颜色数?

Han*_*ans 10

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

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

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

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

有关不同"颜色类型"的更多信息,请参阅以下 链接.

请注意,and class InnerText属性的含义取决于颜色类型.例如,在基于主题的颜色的情况下,属性被设置为集合的索引.ForegroundColorBackgroundColorInnerTextColorScheme

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

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();
}
Run Code Online (Sandbox Code Playgroud)