Open Xml Sdk操作word


最近要做word的相關操作,因為要使用word2007的公式,一種是使用word自帶的Omaths,另一種是open xml sdk.所以有了這一篇文章的出現。這里查閱了相關資料和open xml sdk自帶的幫助文檔 

open xml sdk使用相關的博客:http://blog.csdn.net/songpengpeng20100202/article/category/947920

              http://www.cnblogs.com/xuanhun/tag/openxml/

              http://blog.csdn.net/istarsoft/article/details/5700874

              http://blog.csdn.net/tx_officedev/article/category/934720

                                     http://blog.csdn.net/april_zheng/article/details/6972222

              利用OpenXml生成Word2007文檔

open xml sdk下載地址:http://www.microsoft.com/en-us/download/details.aspx?id=5124

Open xml sdk目前的版本是2.0,在這個版本里的還有Open XML SDK 2.0 Productivity Tool。這個工具的主要目的是對Word、PowerPoint或者Excel文檔進行反向工程。接着,它會生成可以重新創建這個文檔的C#代碼。這個工具還能用來驗證文檔。

首先安裝好open xml sdk,然后先建工程,在工程中引用DocumentFormat.dll,在DocumentFormat.dll可能依賴於Windowsbase.dll里面的System.Io.Packaging.所以這里將Windowsbase.dll也加入引用。

接下來就是開始生成word。

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using DocumentFormat.OpenXml.Packaging;
 6 using DocumentFormat.OpenXml;
 7 using DocumentFormat.OpenXml.Wordprocessing;
 8 using System.Xml;
 9 using System.IO;
10 
11 namespace CreateWordUsingOpenXml
12 {
13     class Program
14     {
15         static void Main(string[] args)
16         {
17             string filePath = "D:\\124.docx";
18             //Create a new Wordprocessing document
19             WordprocessingDocument wordDoc = WordprocessingDocument.Create(filePath,WordprocessingDocumentType.Document);
20 
21             //add the MainDocument part in the new WordProcessing document
22             MainDocumentPart mainDocPart=wordDoc.AddNewPart<MainDocumentPart>("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", "rId1");
23             mainDocPart.Document = new Document();
24 
25             wordDoc.Close();
26 
27 
28         }
29     }
30 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM