2012-05-25 12 views
6

Excelのスプレッドシートでセルの背景色を取得しようとしています。私はOpen XML 2.0 SDKを使用しており、* .xlsxファイルを開き、セル値を取得することができます。ただ自然数PatternFill.BackgroundColor戻ると、私はそれがスタイルのIDだと思うことであり、ここでOpen XML 2.0でExcelでcell-backgroundcolorを取得する

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; 
    } 

私の問題:背景色を取得するための私のコードは次のようです。ない自己定義 - Stylesheet.Colors私はExcelの色「で構築」を使用するので、多分それはだ... ... nullであるため、私の問題は、エラーとそのコードの行が

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

戻り、あります色?!

「backGroundColor-Value」から実際のカラー番号を「計算」できるアイデアはありますか?

+0

クラスSpreadsheetReaderは、OpenXMLの2.5 – Elmue

答えて

10

Excelスプレッドシート内のセルの塗りつぶしパターンは、背景色と前景色の2色からなる です。 フォアグラウンドカラーという用語は、ここで少し誤解を招くことがあります。フォントの ではなく、パターン塗りの前景色です。あなたがソリッドカラー がBackgroundColorオブジェクト がシステムに設定されているように選びだしソリッドカラー値に設定されているセルのreleated PatternFillオブジェクトのForegroundColor性質を有する細胞のバックグラウンドを埋めるたとえば

前景色。 PatternFillオブジェクトのPatternTypeプロパティはPatternValues.Solidに設定されています。

セルの背景(塗りつぶし)のカラー値を取得するには、関連するPatternFillオブジェクトのForegroundColorプロパティを で分析する必要があります。

  1. 自動カラーとシステム依存色
  2. インデックスカラー:あなたは、インスタンスが表す「色の種類」を決定 を有します。
  3. ARGBの色(アルファ、赤、緑、青)
  4. テーマベースの色です。
  5. 色に適用される色合いの値。

「色の種類」の詳細については、 linkを参照してください。

ForegroundColorおよびBackgroundColor クラスのInnerTextプロパティの意味は、色の種類によって異なります。例えば、テーマベースのカラーの場合、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(); 
} 
+0

に存在していないあなたの偉大な答えをいただき、ありがとうございます。追加する点は1つだけです:セルのスタイルを変更しないと、 'int cellStyleIndex =(int)theCell.StyleIndex.Value;'行はヌル例外を引き起こします。私はそれが実際にデフォルトスタイルと "何"スタイル(色など)であるかを、どのようにして確かに知ることができますか?あらかじめThx! – basti

+1

@chiffre:StyleIndexがnullの場合、セルスタイルのインデックス0を使用する必要があると思います(私はいくつかのテストを行いました)。しかし、open xml仕様ではそれに関する注釈は見つかりませんでした。 – Hans

+0

その情報をありがとう。実際には "cell.GetColor"などと呼ぶことができます(実際には動作します)) – basti

関連する問題