动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   .Net → 阅读文章

 用C#实现在Word文档中搜索文本

作者来源: 
阅读 数 220 人次 , 2006-5-23 12:19:00 


  在word应用程序中搜索和替换文本是举手之劳的事情,通过word的对象模型,我们也可以使用编程方式来实现。

   Word的对象模型有比较详细的帮助文档,放在 Office 安装程序目录,office 2003是在Program Files\Microsoft Office\OFFICE11\2052下,文档本身是为VBA提供的,在这个目录下还可以看到所有的office应用程序的VBA帮助。

   打开VBAWD10.CHM,看到word的对象模型,根据以往的使用经验,很容易在Document对象下找到Content属性,该属性会返回一个文档文字部分的Range对象,从这个对象中不难取到所有的文档内容,再用string的IndexOf()方法很容易达到目标。

object filename=""; //要打开的文档路径
string strKey=""; //要搜索的文本
object MissingValue=Type.Missing;

Word.Application wp=new Word.ApplicationClass();
Word.Document wd=wp.Documents.Open(ref filename,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);

if (wd.Content.Text.IndexOf(strKey)>=0)
{
MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);
}
else
{
MessageBox.Show("文档中没有指定的关键字!","搜索结果",MessageBoxButtons.OK);
}


   不过,这种做法是很勉强的,对小文档来说,不存在问题,对超长超大的文档来说,这样的实现方法已经暗埋bug了,而且是程序级的bug,因为正常的测试会很难发现问题,在使用中导致程序出现什么样的结果也很难量化描述。

   其实,在word中已经提供了可以用作搜索的对象Find,在对象模型上也比较容易找到,对应的说明是这样的:该对象代表查找操作的执行条件。Find 对象的属性和方法与“替换”对话框中的选项一致。从模型上看,Find对象是Selection的成员,从示例代码来看似乎也是Range的成员,查找Range的属性,果然如此。于是修改上面的代码:



wd.Content.Find.Text=strKey;
if (wd.Content.Find.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))
{
MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);
}
else
{
MessageBox.Show("文档中没有指定的关键字!","搜索结果",MessageBoxButtons.OK);
}



   这样似乎也不是最好,因为我只要判断指定的文本是不是在文档中,而不需要知道它出现了几次,如果有多个要搜索的文本,难道每次都进行全文档搜索?假设我要搜索的文本包含在文档中,最好的情况是在文档开头就包含我要查找的文本,最坏的情况是在文档的最后包含要查找的文本,如果每次取一部分文档进行判断,符合条件就结束本次搜索,就可以避免每次搜索整个文档了。模型中的Paragraphs对象现在派上用场了,再修改一下代码:


int i=0,iCount=0;
Word.Find wfnd;

if (wd.Paragraphs!=null && wd.Paragraphs.Count>0)
{
iCount=wd.Paragraphs.Count;
for(i=1;i<=iCount;i++)
{
wfnd=wd.Paragraphs[i].Range.Find;
wfnd.ClearFormatting();
wfnd.Text=strKey;
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))
{
MessageBox.Show("文档中包含指定的关键字!","搜索结果",MessageBoxButtons.OK);
break;
}
}
}
  
 本文TagsC#  Word  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用C#创建PDA应用程序的柱形图控件
· 下一篇:用VisualC#.NET编写服务器日期控件
· ASP.NET图象处理详解
· asp.net中一次更新DATAGRID中所有记录
· ASP.NET窗体对话框的实现
· C#中的域(field)和属性(property)
· 用VB构建Internet的应用


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077