注意: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;