【问题标题】:Creating Excel document with OpenXml sdk 2.0使用 OpenXml sdk 2.0 创建 Excel 文档
【发布时间】:2010-11-03 23:53:18
【问题描述】:

我已经使用 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;
} 

【问题讨论】:

    标签: c# excel openxml spreadsheetml


    【解决方案1】:

    注意:OpenXML 2.0 SDK 目前在 CTP 中,直到 Office2010 才获得生产使用许可。

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

    如果您从头开始创建文档,您可以使用 DocumentReflector 为默认 Stylesheet 对象生成代码,然后添加您需要的样式。

    从默认开始:

    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 索引应用于要格式化的单元格: (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;
    

    【讨论】:

      【解决方案2】:

      如何指定单元格样式?

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

      这里的“11U”是 StylesPart.Stylesheet.CellFormats 的从零开始的索引,其中每个 CellFormat 定义了 NumberFormat、Font、Fill 和 Border 样式的组合。

      您不必通过程序添加所有样式,而是可以创建一个模板 xlsx 文件,其中包含您需要的所有格式,然后在程序中指定样式索引。

      【讨论】:

      • 王海凌您好。我不确定您提到的如何修改单元格的样式。您如何获得 StyleIndex = 11U。在我的情况下,我需要的是让单元格有边框。我还需要它有蓝色背景。可以回复stackoverflow.com/questions/15791732/…。提前谢谢你
      【解决方案3】:

      非常感谢这篇文章。

      经过一番努力(和谷歌搜索),我终于设法创建了一个非常易于使用的 C# 类,它接受一个数据集和一个文件名,并创建了一个 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 应用程序,但如果你运行的是 VS2010,你可以让 Visual Studio 升级这个项目。

      享受吧。

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

      【讨论】:

      • 来自您的博客:我没有添加许可证,因为这是免费代码,您可以随意使用。 法律现实:没有许可证表示默认版权法适用(免责声明 - IANAL - 我不是律师)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多