如何在C#中搜索document(.doc)的内容 [英] How can I search content of document(.doc) in C#

查看:156
本文介绍了如何在C#中搜索document(.doc)的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想从C#中的word文档(.doc或.docx)中搜索字符串.我已经将该文档转换为字节数组.但无法搜索.

Hi All,

I want to search a string from word document(.doc or .docx) in C#. I have converted that document in byte array. But unable to search.

//Convert doc file in byte array
string filePath = "C:\\SearchFiles\\Doc1.doc";
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

BinaryReader reader = new BinaryReader(stream);
byte[] file = reader.ReadBytes((int)stream.Length);



我想在该文档中搜索一个字符串(例如:事务").


如何搜索?

请帮忙.

提前谢谢.

[已修改:添加了预标记]



I want to search a string(example : "transaction") in that document.


How can I search?

Please help.

Thanks in advance.

[Modified: added pre tags]

推荐答案

我确定可以这样做,但是Microsoft提供了可以使用的Word对象模型.查找Microsoft.Office.Interop.Word.

可以在以下位置找到Microsoft参考: Microsoft.Office. Interop.Word命名空间 [
I''m sure you could do it that way, but Microsoft has provided the object model for Word that you can use. Look up Microsoft.Office.Interop.Word.

The Microsoft Reference can be found at: Microsoft.Office.Interop.Word Namespace [^]

You can parse the document a lot easier with those classes.


谢谢.
现在我已经完成了.我已经在我的应用程序中添加了Microsoft Word 11.0对象库.
< pre lang ="cs">
对象路径= Path.GetFullPath(filePath);
Word.Application word =新的Word.Application();
Word.Document doc =新的Word.Document();
对象丢失= System.Type.Missing;

doc = word.Documents.Open(参考路径,参考缺失,参考缺失,参考缺失,参考缺失,
裁判缺失,裁判缺失,裁判缺失,裁判缺失,
裁判缺失,裁判缺失,裁判缺失,裁判缺失,
ref丢失,ref丢失,ref丢失);
doc.Activate();
foreach(doc.StoryRanges中的Word.Range tmpRange)
{
tmpRange.Find.Text = searchString;
tmpRange.Find.Wrap = Word.WdFindWrap.wdFindStop;
bool查找= tmpRange.Find.Execute(缺少引用,缺少引用,缺少引用,
裁判缺失,裁判缺失,裁判缺失,裁判缺失,
裁判缺失,裁判缺失,裁判缺失,裁判缺失,
裁判缺失,裁判缺失,裁判缺失,裁判缺失);
如果(发现==真)
{
fileName = fileName + Path.GetFileName(filePath).ToString()+" ;;" ;;
}
}
doc.Close(缺少参考,缺少参考,缺少参考);
word.Application.Quit(缺少引用,缺少引用,缺少引用);

现在,下面的代码正在运行.
Thank you.
Now I have done this. I have added Microsoft Word 11.0 Object Library in my application.
<pre lang="cs">
object path = Path.GetFullPath(filePath);
Word.Application word = new Word.Application();
Word.Document doc = new Word.Document();
object missing = System.Type.Missing;

doc = word.Documents.Open(ref path, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
doc.Activate();
foreach (Word.Range tmpRange in doc.StoryRanges)
{
tmpRange.Find.Text = searchString;
tmpRange.Find.Wrap = Word.WdFindWrap.wdFindStop;
bool find = tmpRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
if (find == true)
{
fileName = fileName + Path.GetFileName(filePath).ToString() + ";";
}
}
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);

Now the below code is working.


这篇关于如何在C#中搜索document(.doc)的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆