码迷,mamicode.com
首页 > 其他好文 > 详细

C#根据关键字检索本地word文档

时间:2014-04-29 22:30:43      阅读:617      评论:0      收藏:0      [点我收藏+]

标签:datagridview   com   http   blog   class   style   div   img   code   java   javascript   

bubuko.com,布布扣
  1        /// <summary>
  2        /// 检索根目录下的子目录及其所有文件,并在datagridview中显示文档名称及路径--递归调用
  3        /// </summary>
  4        /// <param name="rootPath">根目录</param>
  5        /// <param name="strKey">关键字包</param>
  6         private void GetAllFiles(string rootPath,List<string> strKey)
  7         {
  8               DirectoryInfo dir = new DirectoryInfo(rootPath);
  9               string[] dirs = System.IO.Directory.GetDirectories(rootPath);//得到所有子目录
 10              foreach (string di in dirs)
 11              {
 12                  GetAllFiles(di,strKey);
 13              }
 14             FileInfo[] files = dir.GetFiles("*.doc"); //查找文件        
 15             //遍历每个word文档            
 16             foreach (FileInfo fi in files)
 17             {
 18                 string filename = fi.Name;
 19                 string filePath = fi.FullName;
 20                 object filepath = filePath;
 21                 filename = SearchDoc.SearchInDoc(filepath, strKey, filename); //调用检索文档关键字的方法,并返回检索出的文档名称
 22                 if (filename != "")
 23                 {
 24                     dtBGMC.Rows.Add(filename, filepath); //datagridview逐行显示检索出来的结果
 25                 }
 26             }
 27         }
 28 
 29 
 30 
 31 
 32         /// <summary>
 33         /// search in a DOC file(查询DOC文件的内容)
 34         /// </summary>
 35         /// <param name="filepath">文档路径</param>
 36         /// <param name="strKey">要搜索的关键字数组</param>
 37         /// <param name="filename">文档名称</param>
 38         /// <returns></returns>
 39         public static string SearchInDoc(object filepath, List<string> strKey, string filename)
 40         {
 41             string KeyInfilename = "";
 42             object MissingValue = System.Reflection.Missing.Value;//Type.Missing;  
 43             try
 44             {
 45                 wp = new Microsoft.Office.Interop.Word.ApplicationClass();
 46                 wd = wp.Documents.Open(ref filepath, ref MissingValue,
 47                 ref readOnly, ref MissingValue,
 48                 ref MissingValue, ref MissingValue,
 49                 ref MissingValue, ref MissingValue,
 50                 ref MissingValue, ref MissingValue,
 51                 ref MissingValue, ref MissingValue,
 52                 ref MissingValue, ref MissingValue,
 53                 ref MissingValue, ref MissingValue);
 54                 Microsoft.Office.Interop.Word.Find wfnd;
 55 
 56                 if (wd.Paragraphs != null && wd.Paragraphs.Count > 0)
 57                 {
 58                     int keyscount = 0;
 59                     for (int i = 0; i < strKey.Count; i++) //循环关键字数组
 60                     {
 61                         for (int j = 1; j <= wd.Paragraphs.Count; j++)
 62                         {
 63                             wfnd = wd.Paragraphs[j].Range.Find;
 64                             wfnd.ClearFormatting();
 65                             wfnd.Text = strKey[i].ToString();
 66                             if (wfnd.Execute(ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
 67                               ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
 68                               ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
 69                               ref MissingValue, ref MissingValue, ref MissingValue))
 70                             {
 71                                 keyscount++;
 72                                 break;
 73                             }
 74                         }
 75                     }
 76                     if (keyscount == strKey.Count)
 77                     {
 78                         KeyInfilename = filename;
 79                     }
 80                 }
 81             }
 82             catch (Exception ex)
 83             {
 84                 throw new Exception(ex.Message);
 85             }
 86             finally
 87             {
 88                 if (wd != null)
 89                 {
 90                     wd.Close(ref MissingValue, ref MissingValue, ref MissingValue);
 91                     System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
 92                     wd = null;
 93                 }
 94 
 95                 if (wp != null)
 96                 {
 97                     wp.Quit(ref MissingValue, ref MissingValue, ref MissingValue);
 98                     System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
 99                     wp = null;
100                 }
101 
102                 GC.Collect();
103             }
104             return KeyInfilename;
105         }
106     }
107 }
bubuko.com,布布扣

 

C#根据关键字检索本地word文档,布布扣,bubuko.com

C#根据关键字检索本地word文档

标签:datagridview   com   http   blog   class   style   div   img   code   java   javascript   

原文地址:http://www.cnblogs.com/goodgirlmia/p/3696952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!