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 }

 

posted @ 2013-01-25 09:36  上兵伐谋  阅读(8800)  评论(9编辑  收藏  举报