c# - How to search a given word in word file by C#
问题描述
How can I check that a word file is contain a given word or not?
Example: I want to write a function: bool IsContain(string word, string filePath). The function will return true if filePath is contain word. Otherwise it will return false.
This is my solution with Aspose framework. Is there any better solution?
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;
}
}
}
Thanks!
解决方案
遵循使用 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.");
}
推荐阅读
- python - os.listdir 只显示隐藏文件?
- android - 使用通知 Android 销毁当前活动
- javascript - React.forwardRef 与流中的通用 Props 类型
- c# - 如何进行 T-SQL 查询,其中输出的第一个字母等于一个字符
- firebase - 获取多个用户
- yii2 - yii2 kartik fileinput 多个加载的文件预览
- javascript - 如何在 onclick 事件的图像期间添加动画?这是我的代码
- mongodb - Kotlin <--> Mongo 缓存
- python - 通过 Bs4 Python 抓取页面并删除多个空格
- vaadin - 上传按钮的uploadSucceeded方法在单击上传按钮时被触发两次而不是一次