使用Word API打开Word文档

使用Word API(非Openxml)打开Word文档简单示例(必须安装Word)

  1. 首先需要引入参照Microsoft.Office.Interop.Word
  2. 代码示例
 1         public void OpenWord()
 2         {
 3             // Word应用对象
 4             Word.Application wdApp = null;
 5             // Word文档对象
 6             Word.Document wdDoc = null;
 7 
 8             // Word路径
 9             object oWdPath = "XXXXXXXXXXXXXX";
10             // Word设定:缺损项
11             object oMissing = System.Reflection.Missing.Value;
12             // Word设定:不保存
13             object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
14 
15             try
16             {
17                 // 新建Word应用
18                 wdApp = new Word.Application();
19                 // 设置Word应用为可见
20                 wdApp.Visible = true;
21                 // 打开Word文档
22                 wdDoc = wdApp.Documents.Open(ref oWdPath,
23                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
24                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
25                     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
26             }
27             finally
28             {
29 #pragma warning disable
30                 // 关闭Word文档
31                 if (wdDoc != null) { wdDoc.Close(); wdDoc = null; }
32                 // 关闭Word应用
33                 if (wdApp != null) { wdApp.Quit(ref doNotSaveChanges, ref oMissing, ref oMissing); wdApp = null; }
34 #pragma warning disable
35                 GC.Collect();
36             }
37         }

注意:

该方法必须安装Word,运行速度可能比较慢。

如只是要取得Word里面的数据,且速度要求比较高,推荐使用Openxml,而且不需要安装Word。

转载于:https://www.cnblogs.com/zuoyetianqi/archive/2013/03/23/2976864.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值