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