/// <summary>
/// 检索根目录下的子目录及其所有文件,并在datagridview中显示文档名称及路径--递归调用
/// </summary>
/// <param name="rootPath">根目录</param>
/// <param name="strKey">关键字包</param>
private void GetAllFiles(string rootPath,List<string> strKey)
{
DirectoryInfo dir = new DirectoryInfo(rootPath);
string[] dirs = System.IO.Directory.GetDirectories(rootPath);//得到所有子目录
foreach (string di in dirs)
{
GetAllFiles(di,strKey);
}
FileInfo[] files = dir.GetFiles("*.doc"); //查找文件
//遍历每个word文档
foreach (FileInfo fi in files)
{
string filename = fi.Name;
string filePath = fi.FullName;
object filepath = filePath;
filename = SearchDoc.SearchInDoc(filepath, strKey, filename); //调用检索文档关键字的方法,并返回检索出的文档名称
if (filename != "")
{
dtBGMC.Rows.Add(filename, filepath); //datagridview逐行显示检索出来的结果
}
}
} /// <summary>
/// search in a DOC file(查询DOC文件的内容)
/// </summary>
/// <param name="filepath">文档路径</param>
/// <param name="strKey">要搜索的关键字数组</param>
/// <param name="filename">文档名称</param>
/// <returns></returns>
public static string SearchInDoc(object filepath, List<string> strKey, string filename)
{
string KeyInfilename = "";
object MissingValue = System.Reflection.Missing.Value;//Type.Missing;
try
{
wp = new Microsoft.Office.Interop.Word.ApplicationClass();
wd = wp.Documents.Open(ref filepath, ref MissingValue,
ref readOnly, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue);
Microsoft.Office.Interop.Word.Find wfnd; if (wd.Paragraphs != null && wd.Paragraphs.Count > )
{
int keyscount = ;
for (int i = ; i < strKey.Count; i++) //循环关键字数组
{
for (int j = ; j <= wd.Paragraphs.Count; j++)
{
wfnd = wd.Paragraphs[j].Range.Find;
wfnd.ClearFormatting();
wfnd.Text = strKey[i].ToString();
if (wfnd.Execute(ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue))
{
keyscount++;
break;
}
}
}
if (keyscount == strKey.Count)
{
KeyInfilename = filename;
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (wd != null)
{
wd.Close(ref MissingValue, ref MissingValue, ref MissingValue);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wd);
wd = null;
} if (wp != null)
{
wp.Quit(ref MissingValue, ref MissingValue, ref MissingValue);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wp);
wp = null;
} GC.Collect();
}
return KeyInfilename;
}
}
}

C#使用COM搜索本地word文档关键字的更多相关文章

  1. Android开发——使用Jword生成本地word文档

    本文主要介绍如何使用Jword生成本地word文档,这里涉及到Jword的使用技巧,本文给出相应的代码,需要的朋友可以参考下. 为什么使用Jword呢?因为IText .Freemark在安卓平台上压 ...

  2. 用lucene.net根据关键字检索本地word文档

    目前在做一个winform小软件,其中有一个功能是能根据关键字检索本地保存的word文档.第一次是用com读取word方式(见上一篇文章),先遍历文件夹下的word文档,读取每个文档时循环关键字查找, ...

  3. C# 利用Aspose.Words .dll将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)

    下载Aspose.Words .dll  http://pan.baidu.com/s/1c8659k 在vs2010中新建窗体应用程序,命名为 wordtopdf 添加Aspose.Words .d ...

  4. java利用Aspose.words.jar将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)

    package doc; import java.io.*; import junit.framework.Test; import com.aspose.words.*; public class ...

  5. 在项目中利用TX Text Control进行WORD文档的编辑显示处理

    在很多文档管理的功能模块里面,我们往往需要对WORD稳定进行展示.编辑等处理,而如果使用微软word控件进行处理,需要安装WORD组件,而且接口使用也不见得简单易用,因此如果有第三方且不用安装Offi ...

  6. C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例

    C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...

  7. 使用Freemarker创建word文档

    最近做一个项目,本来是直接在网页上查看文本信息,然后给客户直接打印的,但是发现也许是浏览器还是打印机的原因,总之,有个客户打印出来的格式始终与其他的不同,没办法,最后想到了直接将数据库中的信息生成一个 ...

  8. $用python-docx模块读写word文档

    工作中会遇到需要读取一个有几百页的word文档并从中整理出一些信息的需求,比如产品的API文档一般是word格式的.几百页的文档,如果手工一个个去处理,几乎是不可能的事情.这时就要找一个库写脚本去实现 ...

  9. 版本管理 word 文档比较

    1.因为公司还在用SVN, 2.而且 还在用word 写文档, 3.而且 commit log 基本不写, 所以导致,想了解word文档 改动, 很浪费时间!!!! 所以想 快速了解word 改动, ...

随机推荐

  1. JavaScript之三:jQuery插件开发(一)

    在早期的开发中,正如前面闭包中所提到的那样,人们一开始并没有意识到要开发出插件这么个玩意儿,都是遇到啥写啥.在长期的工作中,人们发现很多代码是重复的,写了一遍又一遍,以登录页面为例,每写一次都需要重新 ...

  2. Nginx并发訪问优化

    Nginx反向代理并发能力的强弱,直接影响到系统的稳定性.安装Nginx过程,默认配置并不涉及到过多的并发參数,作为产品执行,不得不考虑这些因素.Nginx作为产品执行,官方建议部署到Linux64位 ...

  3. [Oracle] 分析功能(1)- 语法

    语法概览 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGJhbm90ZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQ ...

  4. 学习笔记之TCP/IP协议的重要性

    1. 随处可见的协议     在计算机网络与信息通信领域里,人们常常提及"协议"一词.互联网中常 用的具有代表性的协议有IP.TCP.HITP等. 而LAN(局域网)中经常使用的协 ...

  5. 基于VMware的虚拟Linux集群搭建-lvs+keepalived

    基于VMware的虚拟Linux集群搭建-lvs+keepalived 本文通过keepalived实现lvsserver的的双机热备和真实server之间的负载均衡.这方面的blog挺多,可是每一个 ...

  6. JavaScript之二:this

    在JavaScript中,this的指代对象是什么?最精辟的解释却只有一句话: when a function of an object was called, the object will be ...

  7. Cache 在选择的几点思考

    Cache为缓解DB装有重要数据,下面经常使用memcached和redis要总结,促进技术的选择. 1 memcached  (1) 有限支持的操作,持经常使用的set.get.delete和过期删 ...

  8. 表与表的关系把RD搞乱了,记一个Procedure中的bug

    就是6张表的关联查询,写了一个存储过程,使用4层for来处理 bug:最后一个for中,两张表的关联条件少了一个,结果数据多查了. 排查办法:使用dbms_output.printline('');每 ...

  9. hdu4453 Looploop 2012年杭州现场赛 Splay

    题意:维护一个圈,实现六个功能,给某位置起的一些数增加某值,反转某一段数,添加删除某些数,移动当前所指的位置, 简单的splay,把圈拆成链,对于每种操作,处理一下. #define inf 0x3f ...

  10. ORACLE在表中添加的目光凝视和读取列

    在ORACLE中给表.列添加凝视以及读取凝视 1.给表填加凝视:SQL>comment on table 表名 is '表凝视"; 2.给列加凝视:SQL>comment on ...