C#使用COM搜索本地word文档关键字
/// <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文档关键字的更多相关文章
- Android开发——使用Jword生成本地word文档
本文主要介绍如何使用Jword生成本地word文档,这里涉及到Jword的使用技巧,本文给出相应的代码,需要的朋友可以参考下. 为什么使用Jword呢?因为IText .Freemark在安卓平台上压 ...
- 用lucene.net根据关键字检索本地word文档
目前在做一个winform小软件,其中有一个功能是能根据关键字检索本地保存的word文档.第一次是用com读取word方式(见上一篇文章),先遍历文件夹下的word文档,读取每个文档时循环关键字查找, ...
- C# 利用Aspose.Words .dll将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)
下载Aspose.Words .dll http://pan.baidu.com/s/1c8659k 在vs2010中新建窗体应用程序,命名为 wordtopdf 添加Aspose.Words .d ...
- java利用Aspose.words.jar将本地word文档转化成pdf(完美破解版 无水印 无中文乱码)
package doc; import java.io.*; import junit.framework.Test; import com.aspose.words.*; public class ...
- 在项目中利用TX Text Control进行WORD文档的编辑显示处理
在很多文档管理的功能模块里面,我们往往需要对WORD稳定进行展示.编辑等处理,而如果使用微软word控件进行处理,需要安装WORD组件,而且接口使用也不见得简单易用,因此如果有第三方且不用安装Offi ...
- C# 动态生成word文档 [C#学习笔记3]关于Main(string[ ] args)中args命令行参数 实现DataTables搜索框查询结果高亮显示 二维码神器QRCoder Asp.net MVC 中 CodeFirst 开发模式实例
C# 动态生成word文档 本文以一个简单的小例子,简述利用C#语言开发word表格相关的知识,仅供学习分享使用,如有不足之处,还请指正. 在工程中引用word的动态库 在项目中,点击项目名称右键-- ...
- 使用Freemarker创建word文档
最近做一个项目,本来是直接在网页上查看文本信息,然后给客户直接打印的,但是发现也许是浏览器还是打印机的原因,总之,有个客户打印出来的格式始终与其他的不同,没办法,最后想到了直接将数据库中的信息生成一个 ...
- $用python-docx模块读写word文档
工作中会遇到需要读取一个有几百页的word文档并从中整理出一些信息的需求,比如产品的API文档一般是word格式的.几百页的文档,如果手工一个个去处理,几乎是不可能的事情.这时就要找一个库写脚本去实现 ...
- 版本管理 word 文档比较
1.因为公司还在用SVN, 2.而且 还在用word 写文档, 3.而且 commit log 基本不写, 所以导致,想了解word文档 改动, 很浪费时间!!!! 所以想 快速了解word 改动, ...
随机推荐
- 于Unity3D调用安卓AlertDialog
例如,下面的示例代码演示 package com.sample.sampletest; import android.app.AlertDialog; import android.content.D ...
- 蜗牛—苍茫IT文章大学的路(十)
昨晚,有个叫***培训机构鼓吹我们学校.起初我还以为是介绍这个游戏吧.谁知道.它原来是一个培训结构.去年我买的表啊 我知道这会不会去,我也浪费了时间审查.因为今天下午和晚上来测试啊.我没有审查,. 当 ...
- zoj 3203 Light Bulb,三分之二的基本问题
Light Bulb Time Limit: 1 Second Memory Limit: 32768 KB Compared to wildleopard's wealthiness, h ...
- Hadoop它——跑start-all.sh时间namenode不启动
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46353211 近期遇到了一个问题,运行start-all.sh的时候发现JPS一下 ...
- Android数据库高手秘籍(六)——LitePal的改动和删除操作
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/40083685 在上一篇文章中,我们学会了使用LitePal进行存储数据的功能.确实 ...
- JBoss7官方最新版下载地址
JBoss是全世界开发人员共同努力的成果,一个基于J2EE的开放源码的应用server. 由于JBoss代码遵循LGPL许可,能够在不论什么商业应用中免费使用它,而不用支付费用.2006年,Jboss ...
- C#实现对mongoDB的简单增删查改
首先添加所需要驱动包(可通过nuget获得) using MongoDB.Bson;using MongoDB.Driver;using MongoDB.Driver.Builders; 一.设置配置 ...
- 多维算法思考(三):AB组合问题
多维算法思考(三):AB组合问题 题目:x个A,y个B可以组合成多少个不同排列的问题. 首先,我们用数学的方式思考,这个问题属于<组合数学>的问题,我们的第一种方法可以用组合思路来求解. ...
- JAVA于Get和Post差异请求
1. get 离server在对数据的访问.post 它是对server数据的传输. get 请求返回 request - URI 随机信息指出,. Post 请求发送电子邮件.观看新闻或交互式用户发 ...
- C#中ISpostback
响应客户端控件时ispostback为true 代码: using System; using System.Collections.Generic; using System.Linq; using ...