c# - 使用 OpenXml sdk 2.0 创建 Excel 文档

标签 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;
} 

最佳答案

注意: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 }, ...

我添加了一个新的 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;

关于c# - 使用 OpenXml sdk 2.0 创建 Excel 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1012547/

相关文章:

c# - 如何使用 Open XML 从 Word 文档中的控件读取数据

sql-server - OPENXML 未返回预期结果

c# - DoEvents 与其他任何东西 --> 对于长时间的 COM 操作

android - 如何在android中将excel表转换成sqlite数据库

c# - 如何在 ExcelInterop 中查找第一个和最后一个单元格以及 C# 中的图形范围

excel - Power Query - 单元格以当前行单元格文本开头的所有行的计数

c# - 'Timer' 上的错误是 'System.Windows.Forms.Timer' 和 'System.Threading.Timer' 之间的模糊引用

c# - "yield return"更紧凑的方式?

c# - VS2013 错误 "Exception has been thrown by the target of an invocation"

c# - 合并单元格上的 OpenXML SDK 边框仅应用于第一个单元格