C#如何在word文件中搜索给定的单词
How to search a given word in word file by C#
如何检查单词文件是否包含给定的单词?
示例:
我想写一个函数:bool IsContain(string word, string filePath)。
如果 filePath 包含 word,该函数将 return 为真。否则会 return false.
这是我使用 Aspose 框架的解决方案。有没有更好的解决方案?
public class FindContentOfWordDoc
{
public bool FindContent(string filePath, string content)
{
var doc = new Document(filePath);
var findReplaceOptions = new FindReplaceOptions
{
ReplacingCallback = new FindCallBack(),
Direction = FindReplaceDirection.Backward
};
var regex = new Regex(content, RegexOptions.IgnoreCase);
doc.Range.Replace(regex, "", findReplaceOptions);
return (findReplaceOptions.ReplacingCallback as FindCallBack)?.IsMatch ?? false;
}
private class FindCallBack : IReplacingCallback
{
public bool IsMatch { get; private set; }
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
IsMatch = true;
return ReplaceAction.Stop;
}
}
}
谢谢!
如果您正在使用评论中所述的 Aspose 库,您可以通过自定义实现 IReplacingCallback 接口来实现。
bool IsContain(string word, string filePath)
{
Document doc = new Document(filePath);
OccurrencesCounter counter = new OccurrencesCounter();
doc.Range.Replace(new Regex(word), counter, false);
return counter.Occurrences > 0;
}
private class OccurrencesCounter : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
mOccurrences++;
return ReplaceAction.Skip;
}
public int Occurrences
{
get { return mOccurrences; }
}
private int mOccurrences;
}
使用 VSTO 执行代码段:
if (Application.Selection.Find.Execute(ref findText,
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))
{
MessageBox.Show("Text found.");
}
else
{
MessageBox.Show("The text could not be located.");
}
如何检查单词文件是否包含给定的单词?
示例: 我想写一个函数:bool IsContain(string word, string filePath)。 如果 filePath 包含 word,该函数将 return 为真。否则会 return false.
这是我使用 Aspose 框架的解决方案。有没有更好的解决方案?
public class FindContentOfWordDoc
{
public bool FindContent(string filePath, string content)
{
var doc = new Document(filePath);
var findReplaceOptions = new FindReplaceOptions
{
ReplacingCallback = new FindCallBack(),
Direction = FindReplaceDirection.Backward
};
var regex = new Regex(content, RegexOptions.IgnoreCase);
doc.Range.Replace(regex, "", findReplaceOptions);
return (findReplaceOptions.ReplacingCallback as FindCallBack)?.IsMatch ?? false;
}
private class FindCallBack : IReplacingCallback
{
public bool IsMatch { get; private set; }
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
IsMatch = true;
return ReplaceAction.Stop;
}
}
}
谢谢!
如果您正在使用评论中所述的 Aspose 库,您可以通过自定义实现 IReplacingCallback 接口来实现。
bool IsContain(string word, string filePath)
{
Document doc = new Document(filePath);
OccurrencesCounter counter = new OccurrencesCounter();
doc.Range.Replace(new Regex(word), counter, false);
return counter.Occurrences > 0;
}
private class OccurrencesCounter : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
mOccurrences++;
return ReplaceAction.Skip;
}
public int Occurrences
{
get { return mOccurrences; }
}
private int mOccurrences;
}
使用 VSTO 执行代码段:
if (Application.Selection.Find.Execute(ref findText,
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))
{
MessageBox.Show("Text found.");
}
else
{
MessageBox.Show("The text could not be located.");
}