我正在制作一个搜索模块(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文档中的单词。
发布于 2018-08-04 18:40:41
有了这个代码,错误看起来是正确的。您尝试打开该文档两次。首先使用"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
}发布于 2014-07-11 15:25:10
我的两个观点是,srObj在这种情况下是完全无关紧要的,你所做的就是绕过并忽略你的docOpen和应用程序对象,你创建了它们,但它们从来没有被使用过。我简单地看了一下API,我知道有一些方法可以获取字符列表和单词集合。我认为您可能需要做的是从您的docOpen属性中获取一个单词集合,并对它们进行筛选。
您可以使用属性docOpen.Words获取或设置单词集合,或使用docOpen.Text获取或设置字符串形式的所有文本。
举个例子
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();我希望这能帮到你。
发布于 2014-07-11 15:25:43
我认为你可以使用Interop库的Find函数来代替stream。您可以使用以下函数来检查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");https://stackoverflow.com/questions/24691347
复制相似问题