您的位置:首页 > 其它

使用Word API打开Word文档

2013-03-23 11:55 281 查看
使用Word API(非Openxml)打开Word文档简单示例(必须安装Word)

首先需要引入参照Microsoft.Office.Interop.Word

代码示例

public void OpenWord()
{
// Word应用对象
Word.Application wdApp = null;
// Word文档对象
Word.Document wdDoc = null;

// Word路径
object oWdPath = "XXXXXXXXXXXXXX";
// Word设定:缺损项
object oMissing = System.Reflection.Missing.Value;
// Word设定:不保存
object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

try
{
// 新建Word应用
wdApp = new Word.Application();
// 设置Word应用为可见
wdApp.Visible = true;
// 打开Word文档
wdDoc = wdApp.Documents.Open(ref oWdPath,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}
finally
{
#pragma warning disable
// 关闭Word文档
if (wdDoc != null) { wdDoc.Close(); wdDoc = null; }
// 关闭Word应用
if (wdApp != null) { wdApp.Quit(ref doNotSaveChanges, ref oMissing, ref oMissing); wdApp = null; }
#pragma warning disable
GC.Collect();
}
}


注意:

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

如只是要取得Word里面的数据,且速度要求比较高,推荐使用Openxml,而且不需要安装Word。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: