概述
ReoGrid 提供内置的文本搜索 API,允许您在一个或多个工作表的单元格中搜索文本。您可以逐个浏览结果、高亮匹配的单元格,或一次获取所有匹配项。
文本搜索类位于 unvell.ReoGrid.TextSearch 命名空间中。
using unvell.ReoGrid.TextSearch;
基本用法
搜索单个工作表
使用 TextSearchSession 与单个 Worksheet 在其中进行搜索。
var session = new TextSearchSession(grid.CurrentWorksheet, "hello");
session.Search();
// 导航到下一个匹配项
var match = session.NextMatch();
if (match != null)
{
grid.CurrentWorksheet.SelectionRange = match.Cell.PositionAsRange;
}
搜索整个工作簿
传入 IWorkbook 实例(网格控件)以搜索所有工作表。
var session = new TextSearchSession(grid, "hello");
session.Search();
var match = session.NextMatch();
if (match != null)
{
match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}
搜索特定工作表
传入工作表列表以限制搜索范围。
var sheets = new List<Worksheet> { grid.Worksheets[0], grid.Worksheets[1] };
var session = new TextSearchSession(sheets, "hello");
session.Search();
浏览结果
调用 Search() 后,使用 NextMatch() 和 PreviousMatch() 浏览结果。当没有匹配项时,两个方法都返回 null。
// 向前
var match = session.NextMatch();
// 向后
var match = session.PreviousMatch();
循环搜索
默认情况下,LoopSearch 为 true,这意味着导航会从最后一个匹配项循环回到第一个(反之亦然)。设置为 false 可以在到达末尾时停止。
session.LoopSearch = false;
var match = session.NextMatch();
if (match == null)
{
MessageBox.Show("No more results.");
}
获取所有匹配项
使用 Matches 属性一次访问完整的结果列表。
var session = new TextSearchSession(grid, "hello");
session.Search();
foreach (var match in session.Matches)
{
Console.WriteLine($"Found at: {match.Cell.Position}, offset: {match.TextIndex}");
}
ITextSearchMatch 接口公开以下属性:
| 属性 | 类型 | 描述 |
|---|---|---|
Cell | Cell | 包含匹配文本的单元格 |
TextIndex | int | 匹配项在单元格显示文本中的字符位置 |
高亮匹配项
使用 HighlightTextSearchSession 可视化高亮所有匹配的单元格。
var session = new HighlightTextSearchSession(grid, "hello", grid.CurrentWorksheet);
session.Search();
// 以金色高亮所有匹配项
session.MarkAllResultHighlight(SolidColor.Goldenrod);
// 导航
var match = session.NextMatch();
if (match != null)
{
match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}
要移除所有高亮(例如搜索关键词变更时):
session.UnmarkAllResultHighlight();
完整示例:查找对话框
以下示例展示如何构建一个具有”下一个”/“上一个”/“查找全部”功能的简单查找对话框。
HighlightTextSearchSession searchSession = null;
HighlightTextSearchSession GetOrCreateSession(string keyword)
{
if (searchSession == null)
{
searchSession = new HighlightTextSearchSession(grid, keyword, grid.CurrentWorksheet);
searchSession.Search();
searchSession.MarkAllResultHighlight(SolidColor.Goldenrod);
searchSession.LoopSearch = true;
}
return searchSession;
}
void OnKeywordChanged(string newKeyword)
{
// 清除上一个会话的高亮
searchSession?.UnmarkAllResultHighlight();
searchSession = null;
}
void FindNext(string keyword)
{
var match = GetOrCreateSession(keyword).NextMatch();
if (match != null)
match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}
void FindPrevious(string keyword)
{
var match = GetOrCreateSession(keyword).PreviousMatch();
if (match != null)
match.Cell.Worksheet.SelectionRange = match.Cell.PositionAsRange;
}
IEnumerable<ITextSearchMatch> FindAll(string keyword)
{
return GetOrCreateSession(keyword).Matches;
}
API 参考
TextSearchSession
| 成员 | 描述 |
|---|---|
TextSearchSession(IWorkbook, string, Worksheet?) | 搜索工作簿中的所有工作表 |
TextSearchSession(IEnumerable<Worksheet>, string, Worksheet?) | 搜索指定的工作表 |
TextSearchSession(Worksheet, string) | 搜索单个工作表 |
void Search() | 执行搜索。必须在浏览结果前调用 |
ITextSearchMatch NextMatch() | 返回下一个匹配项,无匹配时返回 null |
ITextSearchMatch PreviousMatch() | 返回上一个匹配项,无匹配时返回 null |
IEnumerable<ITextSearchMatch> Matches | 所有匹配结果 |
ITextSearchMatch CurrentMatch | 获取或设置当前匹配位置 |
bool LoopSearch | 到达末尾时是否循环(默认值:true) |
string Text | 搜索关键词 |
List<Worksheet> Worksheets | 被搜索的工作表 |
HighlightTextSearchSession
继承自 TextSearchSession,增加单元格高亮功能。
| 成员 | 描述 |
|---|---|
void MarkAllResultHighlight(SolidColor color) | 以指定颜色高亮所有匹配的单元格 |
void UnmarkAllResultHighlight() | 移除所有高亮 |