能够按页号提取word文档文本内容的小程序,由C#实现

来源:互联网 发布:金税通网络电子发票 编辑:程序博客网 时间:2024/05/01 21:50

在以前做过的一个C#项目中,当时有个需求就是按页提取word文档的内容,后来这个需求用不到了,但是从中间摸索出来了一些方法。现在作出的这个程序,对于.doc、.docx、格式word文件,基本都上能够读取,也碰到过特殊文件不能读取的情况,几率很小。

要想操作word文档,在C#中需要引入 Microsoft.Office.Interop.Word.dll, 这个在vs2010中添加引用时直接就就可以找得到,本程序中使用的版本是14.0.0.0.。

直接使用会报错误 无法嵌入互操作类型

将dll文件的属性 “互操作类型”改为false即可解决错误。

程序代码如下:

 1  /// <summary> 2         /// 按页号得到word文件的内容 3         /// </summary> 4         /// <param name="filepath">文件路径</param> 5         /// <param name="pageNum">页号</param> 6         public string getWordContentByPage(string filepath, int pageNum) 7         { 8  9             FileInfo f = new FileInfo(filepath);10             if (!f.Exists)11             {12                 return null;13             }14             string file_name = f.Name;15             string file_path = f.FullName;16             int pageCount = 0;17 18             Microsoft.Office.Interop.Word.Document doc = null;19 20             Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationClass();21 22             object missing = System.Reflection.Missing.Value;23             object FileName = file_path;24 25             object readOnly = true;26             object isVisible = false;27 28             try29             {30                 //打开文档。为什么这么多貌似无用的参数,额,这个我也不知道。。。31                 doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,32                     ref missing, ref missing, ref missing, ref missing, ref missing,33                     ref missing, ref missing, ref missing, ref isVisible, ref missing,34                     ref missing, ref missing, ref missing);35 36                 Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;37 38                 pageCount = doc.ComputeStatistics(stat, ref missing);//得到文档总页数39 40                 object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;41                 object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToNext;42                 object page = pageNum + ""; // 页数43 44                 //ran1 指到要读页数页尾45                 Microsoft.Office.Interop.Word.Range ran1 = doc.GoTo(ref What, ref Which, ref page, ref missing);46 47                 // rand2 为 rand1 的上一页,如果 rand1是首页的话也无妨,这样rand2指向文档头部第一个插入位置48                 Microsoft.Office.Interop.Word.Range ran2 = ran1.GoToPrevious(Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage);49 50                 object objStart = ran2.End; //页首,51 52                 object objEnd = ran1.Start; //页尾53 54                  //如果是最后页则只指定从页首开始读取,页尾值忽略55                   if (page.Equals("" + pageCount))56                   {57                         objStart = ran1.Start;58                         objEnd = missing;59                    }60 61                 Microsoft.Office.Interop.Word.Range r3 = doc.Range(ref objStart, ref objEnd);62                 String content = r3.Text;//此时就已经得到了当前页面的文本内容63 64                 object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;65                 //关闭当前操作文档66                 doc.Close(ref saveOption, ref missing, ref missing);67                 app.Quit(ref saveOption, ref missing, ref missing);68 69                 return content;70             }71             catch (Exception ex)72             {73                 throw ex;74             }75         }

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击