用OpenXml sdk 2.0创buildExcel文档

我已经使用OpenXml SDK 2.0创build了一个Excel文档,现在我必须devise它,但是我不能。

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

我创build一个单元格的代码是:

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的一般方法是创build一个空白文档和一个文档,只需要学习如何实现的function(如背景颜色),然后使用SDK的OpenXmlDiff来查看需要做什么修改该function。

如果要从头开始创build文档,可以使用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; 

非常感谢这篇文章。

经过很多努力(和谷歌search)后,我终于设法创build了一个非常简单易用的C#类,该类使用一个DataSet和一个文件名,并创build一个包含DataSet数据的Office 2007 .xlsx。

突然之间,将函数添加到您的应用程序“导出到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

如何指定单元格样式?

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

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

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