在c#中搜索word文档

本文关键字:word 文档 搜索 | 更新日期: 2023-09-27 18:12:21

我正在制作一个搜索模块(c#中的windows窗体)。它工作良好的。txt文件,但我也需要在word文档中搜索这个单词。我试过用Microsoft.Office.Interop.Word;代码如下

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document docOpen = app.Documents.Open(flname);
StreamReader srObj = new StreamReader(flname);
string read = srObj.ReadToEnd();
if (read.Contains(txtWordInput.Text)) // searching for the input word in the file
{
      count1++;
      lbSearchList.Visible = true;
      lbSearchList.Items.Add(flname);
}
srObj.Close();
app.Documents.Close();

但是它在运行时给出一个错误,即文档文件已经打开,因此即使文档未打开也无法访问。

然后我尝试简单地使用流阅读器工作,它工作并且确实读取了文件,但读取的数据是一些随机符号,而不是实际写在里面的内容。由于这个原因if (read.Contains(txtWordInput.Text))语句无法搜索单词。

请帮助我的代码,如何成功地搜索word文档中的单词

在c#中搜索word文档

有了这些代码,看起来错误是正确的。您尝试打开文档两次。首先使用"app.Documents.Open(flname)"行,然后紧接着创建一个具有相同文件名的StreamReader对象。另外,word文档不是一个文本文件,而是一个包含其他文件的zip文件。所以如果你只是尝试使用StreamReader来读取文件作为文本,你会得到你所得到的…一堆符号。

使用此方法简单地读取文本并在Word文件中搜索特定字符串。还要确保使用正确的using语句。

using Word = Microsoft.Office.Interop.Word;
public static Boolean CheckWordDocumentForString(String documentLocation, String stringToSearchFor, Boolean caseSensitive = true)
{
    // Create an application object if the passed in object is null
    Word.Application winword = new Word.Application();
    // Use the application object to open our word document in ReadOnly mode
    Word.Document wordDoc = winword.Documents.Open(documentLocation, ReadOnly: true);
    // Search for our string in the document
    Boolean result;
    if (caseSensitive)
        result = wordDoc.Content.Text.IndexOf(stringToSearchFor) >= 0;
    else
        result = wordDoc.Content.Text.IndexOf(stringToSearchFor, StringComparison.CurrentCultureIgnoreCase) >= 0;
    // Close the document and the application since we're done searching
    wordDoc.Close();
    winword.Quit();
    return result;
}

然后使用该方法,只需像调用其他静态方法一样调用它。

MyClass.CheckWordDocumentForString(@"C:'Users'CoolDude'Documents'MyWordDoc.docx", "memory", false);

用你的代码应该是这样的:

if (MyClass.CheckWordDocumentForString(flname, txtWordInput.Text, false))
{
    // Do something if it is found
}
else
{
    // Do something if it is not found
}

我的两分钱是srObj在这种情况下是完全无关的,你所做的是绕过和忽略你的docOpen和app对象,你创建它们,但它们从未被使用。我简单地看了一下API,我可以看出有一些方法可以获得字符列表和单词集合。我认为您可能需要做的是从docOpen属性中获取单词集合并筛选它们。

你可以使用docOpen属性。获取或设置单词集合,或docOpen。文本获取或设置所有文本为字符串。

作为一个例子

Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document docOpen = app.Documents.Open(flname);
string read = docOpen.Text
if(read.Contains(txtWordInput.Text)) {
     count1++;
     lbSearchList.Visible = true;
     lbSearchList.Items.Add(flname);
}
app.Documents.Close();

我认为您可以使用互操作库的查找函数而不是流。您可以使用下面的函数来检查word文档中是否存在所需的文本。

    protected bool FindTextInWord(object text, string flname)
    {
        object matchCase = false;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object matchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;
        Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document docOpen = app.Documents.Open(flname);
        bool val = false;
        try
        {
            val = app.Selection.Find.Execute(ref text, ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap,
            ref format, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        }
        finally
        {
            app.Documents.Close();
        }
        return val;
    }

您可以在下面的链接中查看每个参数的详细信息http://msdn.microsoft.com/en-us/library/office/ff193977 (v = office.15) . aspx

你可以像下面这样调用函数

FindTextInWord((object)"Proposal","your file name here");