使用OpenXml sdk 2.0创建Excel文档

JAi*_*iro 16 c# excel openxml spreadsheetml

我使用OpenXml SDK 2.0创建了一个Excel文档,现在我必须设置它的样式,但我不能.

我不知道如何绘制背景颜色或更改不同单元格中的字体大小.

我创建单元格的代码是:

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

fos*_*son 19

注意:OpenXML 2.0 SDK目前处于CTP状态,在Office2010之前未获得生产许可.

我处理OpenXML SDK的一般方法是创建一个空白文档和一个文档,其中只包含您想学习如何实现的功能(如背景颜色),并使用SDK的OpenXmlDiff来查看需要进行哪些更改才能实现功能.

如果要从头开始创建文档,可以使用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 }, ...
Run Code Online (Sandbox Code Playgroud)

我添加了一个大小为12的新字体和一个带红色背景的新填充(索引值为64),并添加了新的CellFormats,它引用了新Font和Fill的索引.(确保也更新计数)

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

然后,在代码中,我将CellStyle索引应用于我想要格式化的单元格:(单元格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;
Run Code Online (Sandbox Code Playgroud)


Mik*_*ill 10

非常感谢这篇文章.

在经历了很多苦苦挣扎(和谷歌搜索)后,我终于设法创建了一个非常易于使用的C#类,它接受DataSet和Filename,并创建一个包含DataSet数据的Office 2007 .xlsx.

突然之间,将应用程序添加到"导出到Excel"的过程变得如此简单......

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

CreateExcelFile.CreateExcelDocument(ds, excelFilename);
Run Code Online (Sandbox Code Playgroud)

我在以下网站上发布了完整的源代码,以及使用它的示例.

它是Visual Studio 2008 C#WinForms应用程序,但如果您运行的是VS2010,则可以让Visual Studio升级此项目.

请享用.

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