Open Xml Sdk操作word

系统 1817 0

最近要做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
        
         }
      

 

Open Xml Sdk操作word


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论