详解C#读取word内容操作

开发 后端
C#读取word内容的具体实现是如何的呢?C#读取word内容所用到的方法有什么呢?那么本文就向你介绍这方面的内容。

C#读取word内容的操作是我们在开发中经常会遇到的问题,那么如何C#读取word内容的操作呢?那么这里向你介绍了7个方面,希望对你有所帮助。

C#读取word内容1:

对项目添加引用,Microsoft Word 11.0 Object Library

C#读取word内容2:

在程序中添加

  1. using Word = Microsoft.Office.Interop.Word;  

C#读取word内容3:

程序中添加

  1. Word.Application app =   
  2. new Microsoft.Office.Interop.Word.Application(); //可以打开word程序  
  3. Word.Document doc = null;  //一会要记录word打开的文档 

word文档和word程序可不是一回事奥!

C#读取word内容4:

一般来说,对于抽取word内容,用的方法很少

  1. public override void openFile(object fileName){} //打开文档  
  2. public override object readPar(int i){} //读取word文档的第i段  
  3. public override int getParCount(){} //返回word文档一共几段  
  4. public override void closeFile(){}  //关闭文档  
  5. public override void quit(){}  //关闭word程序  
  6.  
  7. //从网页上拷贝的目录有时候会出现手动换行符^l,,  
  8. 先将其换成回车段落标记,才能正确读取  
  9. public void replaceChar(){}  

C#读取word内容5:代码

  1. public override void openFile(object fileName)  
  2.  ...{  
  3. try 
  4. ...{  
  5.   if (app.Documents.Count > 0)  
  6.   ...{  
  7.  if (MessageBox.Show(  
  8. "已经打开了一个word文档,  
  9. 你想关闭重新打开该文档吗?", "提示",  
  10.  MessageBoxButtons.YesNo) == DialogResult.Yes)  
  11.  ...{  
  12.    object unknow = Type.Missing;  
  13.    doc = app.ActiveDocument;  
  14.    if (MessageBox.Show(  
  15. "你想保存吗?""保存",  
  16.  MessageBoxButtons.YesNo) == DialogResult.Yes)  
  17.    ...{  
  18. app.ActiveDocument.Save();  
  19.    }  
  20.  
  21.    app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);  
  22.    app.Visible = false;  
  23.  }  
  24.  else 
  25.  ...{  
  26.    return;  
  27.  }  
  28.   }  
  29. }  
  30. catch (Exception)  
  31. ...{  
  32.   //MessageBox.Show("您可能关闭了文档");  
  33.   app = new Microsoft.Office.Interop.Word.Application();  
  34. }  
  35.  
  36. try 
  37. ...{  
  38.   object unknow = Type.Missing;  
  39.   app.Visible = true;  
  40.   doc = app.Documents.Open(ref fileName,  
  41.  ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,  
  42.  ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,  
  43.  ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);  
  44.  }  
  45.  catch (Exception ex)  
  46.  ...{  
  47.    MessageBox.Show("出现错误:" + ex.ToString());  
  48.  }     
  49.       
  50.  }  
  51. public override object readPar(int i)  
  52.  ...{  
  53. try 
  54. ...{  
  55.   string temp = doc.Paragraphs[i].Range.Text.Trim();  
  56.   return temp;  
  57. }  
  58. catch (Exception e) ...{  
  59.   MessageBox.Show("Error:"+e.ToString());  
  60.   return null;  
  61. }  
  62.  }  
  63.  
  64. public override int getParCount()  
  65.  ...{  
  66. return doc.Paragraphs.Count;  
  67.  }  
  68.  
  69. public override void closeFile()  
  70.  ...{  
  71. try 
  72. ...{  
  73.   object unknow = Type.Missing;  
  74.   object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;  
  75.   app.ActiveDocument.Close(  
  76. ref saveChanges, ref unknow, ref unknow);  
  77. }  
  78. catch (Exception ex)  
  79. ...{  
  80.   MessageBox.Show("Error:" + ex.ToString());  
  81. }  
  82.  }  
  83.  
  84. public override void quit()  
  85.  ...{  
  86. try 
  87. ...{  
  88.   object unknow = Type.Missing;  
  89.   object saveChanges = Word.WdSaveOptions.wdSaveChanges;  
  90.   app.Quit(ref saveChanges, ref unknow, ref unknow);  
  91. }  
  92. catch (Exception)  
  93. ...{  
  94.  
  95. }  
  96.  }  
  97.  
  98. public void replaceChar() ...{  
  99. try 
  100. ...{  
  101.   object replaceAll = Word.WdReplace.wdReplaceAll;  
  102.   object missing = Type.Missing;  
  103.  
  104.   app.Selection.Find.ClearFormatting();  
  105.   app.Selection.Find.Text = "^l";  
  106.  
  107.   app.Selection.Find.Replacement.ClearFormatting();  
  108.   app.Selection.Find.Replacement.Text = "^p";  
  109.  
  110.   app.Selection.Find.Execute(  
  111.  ref missing, ref missing,   
  112. ref missing, ref missing, ref missing,  
  113.  ref missing, ref missing,   
  114. ref missing, ref missing, ref missing,  
  115.  ref replaceAll, ref missing,   
  116. ref missing, ref missing, ref missing);  
  117. }  
  118. catch (Exception e)  
  119. ...{  
  120.   MessageBox.Show("文档出现错误,请重新操作");  
  121. }  
  122. }  

C#读取word内容6:

刚才是用读取一段做的例子,如果要读取一句或一篇只需要把doc.Paragraphs[i](readPar中)改成doc.Sentences[i]或doc.content即可,因为都是微软的东东,所以用起来没有一点的障碍,再加上现在的vs2005做的很智能,所以先从java转到了c#上

C#读取word内容7:

实际上,C#读取word内容是不用那么麻烦的,但是如果考虑到可能还要抽取txt,ppt等多种格式,所以就写了一个抽象类,调用起来也方便,这就是为什么我的程序方法开头会有override的原因,总要考虑到通用,所以多了一些代码。

C#读取word内容的基本内容就向你介绍到这里,希望对你了解和学习操作C#读取word内容有所帮助。

【编辑推荐】

  1. C# FileStream写文件解析
  2. C# StreamReader文件处理操作解析
  3. C#追加文件操作解析
  4. C#项目代码规范详解
  5. C#项目初期准备工作浅析
责任编辑:仲衡 来源: 博客园
相关推荐

2009-09-01 11:25:08

C#读取Word文件

2009-08-28 17:34:14

读取word文档

2009-08-19 10:16:15

C#操作Word

2009-09-09 18:20:29

C# XML编程

2009-08-19 10:42:08

C#操作Word表格

2009-09-01 13:10:39

C#读取Word

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:13:49

C#操作Word

2009-08-19 09:42:52

C#操作Word书签

2009-08-28 17:46:18

C#读取Word文档

2009-08-13 09:58:55

C#读取配置文件

2009-08-12 17:19:09

C#读取文件内容

2009-08-19 10:46:48

C#操作Word表格

2009-08-19 10:25:14

C#操作Word

2009-08-19 11:28:41

C#操作Word

2009-08-20 11:01:51

C#操作内存

2009-08-18 16:14:05

C# 操作Excel

2009-08-18 16:42:49

C# 操作XML

2009-08-19 15:55:42

C#操作Access

2009-08-19 11:23:12

C#操作Word
点赞
收藏

51CTO技术栈公众号