C#读取word文档内容 [操作系统入门]
读取word,首先得添加引用,不同的word版本对应着不同的引用
部分版本对应引用如下:
Microsoft Word 11.0 object library对应Office2003
Microsoft Word 12.0 object library对应Office2007
Microsoft Word 14.0 object library对应Office2010
Microsoft Word 15.0 object library对应Office2013
以word 2007为例,故添加Microsoft Word 12.0 Object Library,添加方法,右击项目解决方案,选择 Add Reference,弹出对话框如下图:
然后在namespace上面写下:using Word = Microsoft.Office.Interop.Word;这样,添加引用就算OK了
读取文档代码如下:
protectedstring ReadFile_Word(){
string context="";string path = @"F:测试文档.docx";Word.Application app
= new Microsoft.Office.Interop.Word.Application();Word.Document doc
= null;object unknow = Type.Missing;//object nullobj = System.Reflection.Missing.Value;app.Visible = true;
object file = path;
doc = app.Documents.Open(ref file,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
string temp = doc.Paragraphs[1].Range.Text.Trim();//读取第一段内容
context = doc.Content.Text;//读取整篇文档的内容
doc.Close(ref unknow, ref unknow, ref unknow);//关闭文件
app.Quit(ref unknow, ref unknow, ref unknow);//关闭COM
return context;
}
View Code
C#读取word文档内容
以上是 C#读取word文档内容 [操作系统入门] 的全部内容, 来源链接: utcz.com/z/519437.html