2009-06-18 14 views
15

के साथ एक्सेल दस्तावेज़ बनाना मैंने ओपनएक्सएमएल एसडीके 2.0 का उपयोग कर एक एक्सेल दस्तावेज़ बनाया है, अब मुझे इसे स्टाइल करना है, लेकिन मैं नहीं कर सकता।ओपनएक्सएमएल एसडीके 2.0

मुझे नहीं पता कि कैसे पृष्ठभूमि रंग पेंट करना है या विभिन्न कोशिकाओं में फ़ॉन्ट आकार को बदलना है।

मेरे एक सेल बनाने के लिए कोड है:

private static Cell CreateTextCell(string header, string text, UInt32Value index) 
{ 
    Cell c = new Cell(); 
    c.DataType = CellValues.InlineString; 
    c.CellReference = header + index; 
    InlineString inlineString = new InlineString(); 
    DocumentFormat.OpenXml.Spreadsheet.Text t = new DocumentFormat.OpenXml.Spreadsheet.Text(); 
    t.Text = text; 
    inlineString.AppendChild(t); 
    c.AppendChild(inlineString); 
    return c; 
} 

उत्तर

18

नोट: OpenXML 2.0 एसडीके CTP में है और Office2010 तक उत्पादन में इस्तेमाल के लिए लाइसेंस नहीं है।

ओपनएक्सएमएल एसडीके से निपटने के लिए मेरा सामान्य पद्धति एक रिक्त दस्तावेज़ और एक दस्तावेज़ बनाना है जिसमें आप सीखना चाहते हैं कि कैसे कार्यान्वित करना है (पृष्ठभूमि रंग की तरह) और एसडीके के ओपनएक्सएमएलडीफ़ का उपयोग यह देखने के लिए कि किन परिवर्तनों की आवश्यकता है सुविधा को लागू करने के लिए बनाया जाना चाहिए।

आप स्क्रैच से एक दस्तावेज़ पैदा कर रहे हैं, तो आप DocumentReflector का उपयोग डिफ़ॉल्ट स्टाइलशीट वस्तु के लिए कोड उत्पन्न करने के लिए और फिर शैलियों आप की जरूरत जोड़ सकते हैं।

डिफ़ॉल्ट के साथ शुरू:

new Stylesheet(
new Fonts(
    new Font(
     new FontSize() { Val = 10D }, 
     new Color() { Theme = (UInt32Value)1U }, 
     new FontName() { Val = "Arial" }, 
     new FontFamilyNumbering() { Val = 2 }) 
) { Count = (UInt32Value)1U }, 
new Fills(
    new Fill(
     new PatternFill() { PatternType = PatternValues.None }), 
    new Fill(
     new PatternFill() { PatternType = PatternValues.Gray125 }) 
) { Count = (UInt32Value)2U }, 
new Borders(... 
... 
... 
new CellFormats(
new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }) { Count = (UInt32Value)1U }, ... 

मैं नए CellFormats कि नए के सूचकांक संदर्भ आकार 12 का एक नया फ़ॉन्ट जोड़ दिया है और लाल रंग की पृष्ठभूमि (इंडेक्स मूल्य 64) के साथ एक नया भरें, और जोड़ा फ़ॉन्ट और भरें। (भी गिनता अपडेट करना सुनिश्चित करें)

new Stylesheet(
    new Fonts(
     new Font(
      new FontSize() { Val = 10D }, 
      new Color() { Theme = (UInt32Value)1U }, 
      new FontName() { Val = "Arial" }, 
      new FontFamilyNumbering() { Val = 2 }), 
     new Font(
      new FontSize() { Val = 12D }, 
      new Color() { Theme = (UInt32Value)1U }, 
      new FontName() { Val = "Arial" }, 
      new FontFamilyNumbering() { Val = 2 }) 
      ) { Count = (UInt32Value)2U }, 
    new Fills(
     new Fill(
      new PatternFill() { PatternType = PatternValues.None }), 
     new Fill(
      new PatternFill() { PatternType = PatternValues.Gray125 }), 
     new Fill(
      new PatternFill() { PatternType = PatternValues.Solid, ForegroundColor = new ForegroundColor() { Rgb = "FFFF0000" }, BackgroundColor = new BackgroundColor() { Indexed = 64 } }) 
      ) { Count = (UInt32Value)3U }, 
    new Borders(
     new Border(
      new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder()) 
    ) { Count = (UInt32Value)1U }, 
    new CellStyleFormats(
     new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U } 
    ) { Count = (UInt32Value)1U }, 
    new CellFormats(
     new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }, 
     new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)1U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U }, 
     new CellFormat() { NumberFormatId = (UInt32Value)0U, FontId = (UInt32Value)0U, FillId = (UInt32Value)2U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U } 
    ) { Count = (UInt32Value)3U }, 
    new CellStyles(
     new CellStyle() { Name = "Normal", FormatId = (UInt32Value)0U, BuiltinId = (UInt32Value)0U } 
    ) { Count = (UInt32Value)1U }, 
    new DifferentialFormats() { Count = (UInt32Value)0U }, 
    new TableStyles() { Count = (UInt32Value)0U, DefaultTableStyle = "TableStyleMedium9", DefaultPivotStyle = "PivotStyleLight16" }); 

फिर, कोड में, मैं CellStyle सूचकांक कोशिकाओं के लिए लागू मैं स्वरूपित करना चाहते हैं:। (वहां पहले से ही कोशिकाओं में डेटा ए 2 और ए 3 था सेल A2 हो जाता है बड़ा आकार, ए 3 लाल पृष्ठभूमि हो जाता है)

SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); 
sheetData.Descendants<Row>().Where(r => r.RowIndex == 2U).First().Descendants<Cell>().First().StyleIndex = 1U; 
sheetData.Descendants<Row>().Where(r => r.RowIndex == 3U).First().Descendants<Cell>().First().StyleIndex = 2U; 
+0

+1 आपको बहुत बहुत धन्यवाद। आपकी पोस्ट ने मुझे वास्तव में अच्छी शुरुआत की। – horgh

2

सेल शैली कैसे निर्दिष्ट करें?

new Cell() { CellReference = "B6", StyleIndex = 11U } 

यहाँ "11U" StylesPart.Stylesheet.CellFormats का एक शून्य आधारित सूचकांक, जिसमें प्रत्येक CellFormat NumberFormat, फ़ॉन्ट का एक संयोजन को परिभाषित करता है, भरें और सीमा शैलियों है।

आप की जरूरत नहीं है इस कार्यक्रम के द्वारा सभी शैलियों को जोड़ने के लिए, के बजाय आप सभी स्वरूपों आप इसे में आवश्यकता के साथ एक टेम्पलेट xlsx फ़ाइल बना सकते हैं, और फिर अपने कार्यक्रम में शैली सूचकांक निर्दिष्ट करें।

+0

हैलो हैलाइंग वांग। मुझे यकीन नहीं है कि आपने सेल की शैली को संशोधित करने के तरीके के बारे में क्या बताया है। आप StyleIndex = 11U कैसे प्राप्त करते हैं। मेरे मामले में मुझे क्या चाहिए कि सेल की सीमाएं हों। मुझे नीली पृष्ठभूमि रखने की भी आवश्यकता है। क्या आप http://stackoverflow.com/questions/15791732/openxml-sdk-having-borders-for-cell का उत्तर दे सकते हैं। अग्रिम में धन्यवाद –

9

इस आलेख के लिए बहुत धन्यवाद।

संघर्ष (और Googling) का एक बहुत बाद, मैं अंत में एक बहुत आसान से उपयोग सी # वर्ग है, जो एक डेटासेट और एक फ़ाइल का नाम लेता है बनाने में कामयाब रहे, और किसी Office बनाता है 2007 डेटासेट के डेटा युक्त .xlsx ।

अचानक, "एक्सेल में निर्यात करें" करने के लिए अपने आवेदन करने के लिए एक समारोह जोड़ने की प्रक्रिया के रूप में के रूप में आसान हो जाता है ...

DataSet ds = CreateSampleData();     // Your code here ! 
string excelFilename = "C:\\Sample.xlsx"; 

CreateExcelFile.CreateExcelDocument(ds, excelFilename); 

मैं पूर्ण स्रोत कोड पोस्ट किया है, के साथ साथ इसे का उपयोग का एक उदाहरण , निम्नलिखित वेबसाइट पर।

यह एक विजुअल स्टूडियो 2008 सी # WinForms आवेदन है, लेकिन आप दृश्य स्टूडियो इस परियोजना के उन्नयन, आप VS2010 चला रहे हैं कर सकते हैं।

आनंद लें।

http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

+0

अपने ब्लॉग से: * मैंने लाइसेंस नहीं जोड़ा है, क्योंकि यह आपकी इच्छानुसार उपयोग करने के लिए मुफ्त कोड है। * कानूनी वास्तविकता: * लाइसेंस की अनुपस्थिति का अर्थ है कि डिफ़ॉल्ट कॉपीराइट कानून लागू होते हैं * (अस्वीकरण - इनाल - मैं वकील नहीं हूं) –

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