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 改动, ...
随机推荐
- Big ball of Mud
Big ball of Mud 第一种死法:Big ball of Mud 架构里最常用的反面案例是 big ball of mud.很大程度上可以说打格子,把复杂的系统拆解成小格子是架构师最重要的工 ...
- 思考的工作方式——计划经济or市场经济
背景:单位成立了技术领先的基础部门.专注于产品规划的技术解决方案部门.产品的发展规划方向.批准的项目和各部门的其他工作方案.工作内容是在这一领域没有问题.毕竟,从过去企业发展的一个部门模型现在是一个功 ...
- 【C++ Primer每天刷牙】一间 迭代器
迭代器的介绍 概述 迭代器是一种检查容器内元素并遍历元素的数据类型. 迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或所有元素,每一个迭代器对象代表容器中的确定的地址.迭代 ...
- atitit.为什么技术的选择方法java超过.net有前途
atitit.为什么技术的选择方法java超过.net有前途 #----有没有法律依据不同的铜需求... 通常有开发效率,需要在稳定性.. 笔者 老哇爪 Attilax 艾龙, EMAIL:1466 ...
- POI使用汇总
POI它是apache下一步行为office有关格源工具.我们近期在使用这个框架实现Excel格式数据的导入和导出功能.这里进行一个使用总结.POI提供两种读写API模型:事件模型(eventmode ...
- Scrapy研究和探索(五岁以下儿童)——爬行自己主动多页(抢别人博客所有文章)
首先.在教程(二)(http://blog.csdn.net/u012150179/article/details/32911511)中,研究的是爬取单个网页的方法.在教程(三)(http://blo ...
- dotNET跨平台相关文档
dotNET跨平台相关文档整理 一直在从事C#开发的相关技术工作,从C# 1.0一路用到现在的C# 6.0, 通常情况下被局限于Windows平台,Mono项目把我们C#程序带到了Windows之外的 ...
- Lampda或Linq中的SqlFunctions.StringConvert()
要使用 Lampda或Linq中的SqlFunctions.StringConvert(), 需要引用命名空间using System.Data.Objects.SqlClient, 该函数可进行模拟 ...
- linux安装和配置 mysql、redis 过程中遇到的问题记录(转)
章节目录 mysql redis linux下部署mysql和redis网上的教程很多,这里记录一下我部署.配置的过程中遇到的一些问题和解决办法. mysql ①安装完成后启动的时候报错 Starti ...
- debugging python with IDLE
1. start IDLE "Python 2.5"→"IDLE(Python GUI)" 2. open your source file window Fr ...