سؤال

لقد قمت بإنشاء مستند Excel باستخدام OpenXml SDK 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;
} 
هل كانت مفيدة؟

المحلول

ملحوظة: OPENXML 2.0 SDK حاليا في CTP وغير مرخص للاستخدام الإنتاج حتى Office2010 أ.

وبلدي methodoloy العام للتعامل مع OPENXML SDK هو إنشاء مستند فارغ وثيقة مع مجرد الميزات التي ترغب في معرفة كيفية تنفيذ (مثل لون الخلفية) واستخدام OpenXmlDiff في SDK لمعرفة ما هي التغييرات التي يجب أن تكون المحرز في تنفيذ هذه الميزة.

إذا كنت تقوم بإنشاء وثيقة من الصفر، يمكنك استخدام 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 }, ...

ولقد أضاف الخط الجديد من حجم 12 وملء جديدة مع خلفية حمراء (القيمة المفهرسة 64)، وأضاف CellFormats الجديدة التي تشير إلى مؤشر الخط الجديد وتعبئة. (تأكد من تحديث التهم جدا)

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 إلى خلايا I تريد تنسيقه: (وكان هناك بالفعل البيانات في الخلايا A2 و A3. الخلية A2 يحصل على أكبر حجما، A3 يحصل على خلفية حمراء)

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;

نصائح أخرى

الكثير من الشكر على هذه المقاله.

وبعد الكثير من الكفاح (والبحث في Google)، تمكنت أخيرًا من إنشاء ملف جداً فئة C# سهلة الاستخدام، والتي تأخذ DataSet واسم ملف، وتقوم بإنشاء Office 2007 .xlsx الذي يحتوي على بيانات DataSet.

فجأة، أصبحت عملية إضافة وظيفة إلى التطبيق الخاص بك إلى "التصدير إلى Excel" سهلة مثل...

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

CreateExcelFile.CreateExcelDocument(ds, excelFilename);

لقد قمت بنشر كود المصدر الكامل، بالإضافة إلى مثال لاستخدامه، على الموقع التالي.

إنه تطبيق Visual Studio 2008 C# WinForms، ولكن يمكنك السماح لـ Visual Studio بترقية هذا المشروع، إذا كنت تقوم بتشغيل VS2010.

يتمتع.

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

وكيفية تحديد نمط الخلية؟

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

وهنا "11U" هو مؤشر الصفرية من StylesPart.Stylesheet.CellFormats، في كل CellFormat يحدد مجموعة من NumberFormat، الخط، تعبئة والأساليب الحدود.

وليس لديك لإضافة كل الأنماط من قبل برنامج، بدلا من ذلك يمكنك إنشاء ملف XLSX القالب مع جميع الأشكال التي تحتاج إليها فيه، ومن ثم تحديد مؤشر النمط في البرنامج.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top