如何在C#中阅读Microsoft Word文档. [英] how to read a microsoft word document in c# .

查看:116
本文介绍了如何在C#中阅读Microsoft Word文档.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好;

谁能告诉我们.如何在c#中读取Microsoft Word文档.

hello ;

can anyone tell us. how to read a microsoft word document in c#.

推荐答案

我们可能已经使用FileStream从文本文件中读取文本,但是从词中获取文本的方式不同文档.

我们必须使用一个称为"Microsoft Word 9.0对象库"的Microsoft COM组件,该组件提供了从Word文档中读取的类和方法.

我们必须使用Word.ApplicationClass才能访问单词application.

打开内存中的word文档,将所有内容复制到剪贴板,然后我们就可以从剪贴板中获取数据了.

所需的代码如下:

Word.ApplicationClass wordApp =新的ApplicationClass();

目标文件=路径;

object nullobj = System.Reflection.Missing.Value;

Word.Document doc = wordApp.Documents.Open(

ref文件,ref nullobj,ref nullobj,

ref nullobj,ref nullobj,ref nullobj,

ref nullobj,ref nullobj,ref nullobj,

ref nullobj,ref nullobj,ref nullobj);

doc.ActiveWindow.Selection.WholeStory();

doc.ActiveWindow.Selection.Copy();

IDataObject data = Clipboard.GetDataObject();

txtFileContent.Text = data.GetData(DataFormats.Text).ToString();

doc.Close();
We may have used FileStream to read text from a text file but not the same way for getting text from a word document.

We have to use a Microsoft COM component called "Microsoft Word 9.0 object library" which provides classes and methods to read from a word document.

We have to use Word.ApplicationClass to have access to the word application.

Open the word document in memory, copy all the content to the clipboard and then we can take the data from the clipboard.

The code required is given below:

Word.ApplicationClass wordApp=new ApplicationClass();

object file=path;

object nullobj=System.Reflection.Missing.Value;

Word.Document doc = wordApp.Documents.Open(

ref file, ref nullobj, ref nullobj,

ref nullobj, ref nullobj, ref nullobj,

ref nullobj, ref nullobj, ref nullobj,

ref nullobj, ref nullobj, ref nullobj);

doc.ActiveWindow.Selection.WholeStory();

doc.ActiveWindow.Selection.Copy();

IDataObject data=Clipboard.GetDataObject();

txtFileContent.Text=data.GetData(DataFormats.Text).ToString();

doc.Close();


http://www .c-sharpcorner.com/UploadFile/mgold/WordFromDotNet11082005235506PM/WordFromDotNet.aspx [ ^ ]

请访问上面的链接
http://www.c-sharpcorner.com/UploadFile/mgold/WordFromDotNet11082005235506PM/WordFromDotNet.aspx[^]

please visit the above link


这篇关于如何在C#中阅读Microsoft Word文档.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆