2012-05-25 13 views
6

के साथ Excel में सेल-पृष्ठभूमि रंग प्राप्त करना मैं एक्सेल-स्प्रेडशीट में सेल के पृष्ठभूमि रंग प्राप्त करने का प्रयास कर रहा हूं। मैं ओपन एक्सएमएल 2.0 एसडीके का उपयोग कर रहा हूं और मैं * .xlsx-file खोलने और उदाहरण के लिए सेल-वैल्यू प्राप्त करने में सक्षम हूं।ओपन एक्सएमएल 2.0

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 रिटर्न सिर्फ एक प्राकृतिक संख्या, मुझे लगता है कि यह शैली की आईडी है: पृष्ठभूमि रंग प्राप्त करने के लिए मेरे कोड निम्नलिखित है। मेरी समस्या यह है कि कोड

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

एक त्रुटि के साथ रिटर्न की लाइन है, क्योंकि Stylesheet.Colorsnull है ... ... शायद यह है क्योंकि मैं एक एक्सेल में रंग "में बनाया" का इस्तेमाल किया है - नहीं एक आत्म परिभाषित रंग?!

कोई विचार है कि मैं "बैकग्राउंडकोलर-वैल्यू" से वास्तविक रंग-संख्या कैसे "गणना" कर सकता हूं?

+0

वर्ग SpreadsheetReader OpenXML 2.5 – Elmue

उत्तर

10

एक्सेल स्प्रेडशीट में एक सेल का भरण पैटर्न दो रंगों से बना है: पृष्ठभूमि रंग और अग्रभूमि रंग। फोरग्राउंड रंग शब्द थोड़ा भ्रामक है। यह फ़ॉन्ट का रंग नहीं है बल्कि पैटर्न भरने का अग्रभूमि रंग है।

उदाहरण के लिए यदि आप एक ठोस रंग सेल चुना ठोस रंग मूल्य के रूप में BackgroundColor वस्तु व्यवस्था करने के लिए सेट कर दिया जाता है, जहां पर सेट है की releated PatternFill वस्तु की ForegroundColor संपत्ति के साथ एक सेल की पृष्ठभूमि को भरने अग्रभूमि रंग। PatternType PatternFill ऑब्जेक्ट की संपत्ति PatternValues.Solid पर सेट है।

तो, अपने सेल पृष्ठभूमि (ठोस भरने) का रंग मान प्राप्त करने के लिए, आपको जारी की गई PatternFill ऑब्जेक्ट की संपत्ति का विश्लेषण करना होगा। आप करने के लिए है "रंग के प्रकार के" निर्धारित उदाहरण का प्रतिनिधित्व करता है:

  1. एक स्वत: रंग और प्रणाली निर्भर रंग
  2. एक अनुक्रमित रंग।
  3. एक एआरबीबी रंग (अल्फा, लाल, हरा और नीला)
  4. थीम आधारित रंग।
  5. रंग पर एक टिंट वैल्यू लागू होता है।

विभिन्न "रंग प्रकारों" के बारे में अधिक जानकारी के लिए निम्नलिखित link देखें।

कृपया ध्यान दें कि ForegroundColor और BackgroundColor वर्ग की संपत्ति रंग प्रकार पर निर्भर करती है। उदाहरण के लिए थीम आधारित रंग के मामले में 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

में नहीं मौजूद है करता है अपने महान उत्तर के लिए धन्यवाद।जोड़ने के लिए सिर्फ एक बिंदु: यदि मैं किसी सेल की शैली नहीं बदलता, तो रेखा 'int cellStyleIndex = (int) theell.StyleIndex.Value;' शून्य-अपवाद का कारण बनता है। मैं निश्चित रूप से कैसे जान सकता हूं कि यह वास्तव में डिफ़ॉल्ट शैली और "क्या" शैली (रंग इत्यादि) है? अग्रिम में Thx! – basti

+1

@chiffre: मुझे लगता है कि (मैंने कुछ परीक्षण किया है) यदि स्टाइल इंडेक्स शून्य है तो आपको सेल स्टाइल इंडेक्स 0 का उपयोग करना होगा। हालांकि मुझे ओपन एक्सएमएल विनिर्देशन में इसके बारे में कोई नोट नहीं मिला। – Hans

+0

उस जानकारी के लिए धन्यवाद। वास्तव में एक अच्छे और पूर्ण रैपर की आवश्यकता है - जहां आप बस "सेल.गेटकोलर" आदि कह सकते हैं .. (और वह वास्तव में काम करता है;)) – basti

संबंधित मुद्दे