Open XML SDK 2.5 for Office
Open XML 是可由不同平台上的多个应用程序自由实现的字处理文档、演示文稿和电子表格的开放式标准。Open XML 旨在如实表示用 Microsoft Office 应用程序定义的二进制格式进行编码的现有字处理文档、演示文稿和电子表格。使用 Open XML 的原因很简单:现在存在数以亿计的文档,但遗憾的是,这些文档中的信息与创建文档的程序紧密耦合。Open XML 标准的目的是分离由 Microsoft Office 应用程序创建的文档,以便其他应用程序可以独立于专有格式操作这些文档且不会丢失数据
1.下载安装:
https://www.microsoft.com/en-us/download/details.aspx?id=30425
1.OpenXMLSDKV25.msi 安装完成后目录出现 DocumentFormat.OpenXml.dll
2.OpenXMLSDKToolV25.msi 这是OpenXMLTool可以打开Office 2007以上的版本并且生成标准Office XML格式和标准c#源代码工具。
OpenXMLSDKV25.msi 2.5 MB | 2.5 MB | |
OpenXMLSDKToolV25.msi 24.9 MB | 24.9 MB |
使用 Open XML SDK 中的类
Open XML SDK 2.5 中的类使用起来很简单。在安装了 Open XML SDK 2.5 之后,请在 Visual Studio 中打开现有的项目或应用程序,或者创建新的项目或应用程序。然后在您的项目或应用程序中,添加对以下组件的引用。
-
DocumentFormat.OpenXml
-
WindowsBase
3.案例
处理段落 (Open XML SDK)
public static void WriteToWordDoc(string filepath, string txt){ // Open a WordprocessingDocument for editing using the filepath. using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true)) { // Assign a reference to the existing document body. Body body = wordprocessingDocument.MainDocumentPart.Document.Body; // Add a paragraph with some text. Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text(txt)); }}
使用连续文本 (Open XML SDK)
public static void WriteToWordDoc(string filepath, string txt){ // Open a WordprocessingDocument for editing using the filepath. using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true)) { // Assign a reference to the existing document body. Body body = wordprocessingDocument.MainDocumentPart.Document.Body; // Add new text. Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); // Apply bold formatting to the run. RunProperties runProperties = run.AppendChild(new RunProperties(new Bold())); run.AppendChild(new Text(txt)); }}
使用 WordprocessingML 表 (Open XML SDK)
public static void InsertTableInDoc(string filepath){ // Open a WordprocessingDocument for editing using the filepath. using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true)) { // Assign a reference to the existing document body. Body body = wordprocessingDocument.MainDocumentPart.Document.Body; // Create a table. Table tbl = new Table(); // Set the style and width for the table. TableProperties tableProp = new TableProperties(); TableStyle tableStyle = new TableStyle() { Val = "TableGrid" }; // Make the table width 100% of the page width. TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct }; // Apply tableProp.Append(tableStyle, tableWidth); tbl.AppendChild(tableProp); // Add 3 columns to the table. TableGrid tg = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn()); tbl.AppendChild(tg); // Create 1 row to the table. TableRow tr1 = new TableRow(); // Add a cell to each column in the row. TableCell tc1 = new TableCell(new Paragraph(new Run(new Text("1")))); TableCell tc2 = new TableCell(new Paragraph(new Run(new Text("2")))); TableCell tc3 = new TableCell(new Paragraph(new Run(new Text("3")))); tr1.Append(tc1, tc2, tc3); // Add row to the table. tbl.AppendChild(tr1); // Add the table to the document body.AppendChild(tbl); }}
参考:
https://msdn.microsoft.com/zh-cn/library/office/bb448854.aspx
https://msdn.microsoft.com/ZH-CN/library/office/gg278313.aspx
https://www.microsoft.com/en-us/download/details.aspx?id=30425