Find and Replace in Blazor Spreadsheet
22 Sep 202613 minutes to read
The Blazor Spreadsheet Editor component provides Find and Replace functionality that helps you search for target text and replace the found text with alternative text within a sheet or workbook. You can use the AllowFindAndReplace property to enable or disable Find and Replace functionality.
- The default value for
AllowFindAndReplaceproperty istrue.
Find
Find is used to select the matched contents of a cell within a sheet or workbook. It is extremely useful when working with large data sets.
Find via the UI
Find can be done by any of the following ways:
- In the Home tab of the Ribbon, click the Search icon. The Find and Replace dialog open.
- Enter the text to search for in the Find what box.
- Expand the dialog options and choose any of the following to refine the search:
| Option | Description |
|---|---|
| Search within | Search the target within the active Sheet (default) or in the entire Workbook. |
| Search by | Search either By Rows (default) or By Columns. |
| Match case | Find the matched value with case sensitivity. |
| Match exact cell contents | Find the exact matched cell value with entire cell match. |
- Click Find Next or Previous. The active cell navigates to the next or previous matching occurrence. Click Find Next or Previous repeatedly to cycle through all the occurrences.
Find programmatically
The FindAsync method searches for the specified text in the spreadsheet and automatically navigates the active cell to the next or previous matching occurrence. The available parameters in the FindAsync() method are:
| Parameter | Type | Description |
|---|---|---|
| searchText | string | Specifies the text to search for. This parameter is required and cannot be null, empty, or contain only whitespace. |
| searchScope | SearchScope (optional) | Specifies whether to search in the current sheet or the entire workbook. Accepts values from the SearchScope enumeration. If unspecified, the default is SearchScope.CurrentSheet.Possible values: • SearchScope.CurrentSheet – Searches only within the currently active worksheet.• SearchScope.EntireWorkbook – Searches across all worksheets in the workbook. |
| matchCase | bool (optional) | Specifies whether the search should be case-sensitive. If unspecified, the default is false(case-insensitive). |
| matchEntireCell | bool (optional) | Specifies whether the search should match the entire cell content or allow partial matches. If unspecified, the default is false (partial matches allowed). |
| searchDirection | SearchDirection (optional) | Specifies the order in which cells are searched. Accepts values from the SearchDirection enumeration. If unspecified, the default is SearchDirection.ByRows.Possible values: • SearchDirection.ByRows – Searches cells row by row (left to right, top to bottom).• SearchDirection.ByColumns – Searches cells column by column (top to bottom, left to right). |
| findOption | FindOption (optional) | Controls the navigation behavior between search results. Accepts values from the FindOption enumeration. If unspecified, the default is FindOption.Next.Possible values: • FindOption.Next – Navigates to the next matching occurrence.• FindOption.Previous – Navigates to the previous matching occurrence. |
@page "/"
@using Syncfusion.Blazor.Spreadsheet
@using Syncfusion.Blazor.Buttons
<SfButton OnClick="FindMatch" Content="Find"></SfButton>
<SfSpreadsheet @ref="SpreadsheetInstance" DataSource="DataSourceBytes">
<SpreadsheetRibbon></SpreadsheetRibbon>
</SfSpreadsheet>
@code {
public byte[] DataSourceBytes { get; set; }
public SfSpreadsheet SpreadsheetInstance;
protected override void OnInitialized()
{
string filePath = "wwwroot/Sample.xlsx";
DataSourceBytes = File.ReadAllBytes(filePath);
}
public async Task FindMatch()
{
await SpreadsheetInstance.FindAsync(
searchText: "Error",
searchScope: SearchScope.EntireWorkbook,
matchCase: true,
matchEntireCell: false,
searchDirection: SearchDirection.ByColumns,
findOption: FindOption.Previous);
}
}Find all programmatically
The FindAllAsync method searches for all occurrences of the specified text in the spreadsheet and returns a string array of sheet-qualified cell addresses (for example, "Sheet1!A1"). This method does not navigate the active cell. The available parameters in the FindAllAsync() method are:
| Parameter | Type | Description |
|---|---|---|
| searchText | string | Specifies the text to search for. If this parameter is null, empty, or contains only whitespace, an empty array is returned. |
| searchScope | SearchScope (optional) | Specifies whether to search in the current sheet or the entire workbook. Accepts values from the SearchScope enumeration. If unspecified, the default is SearchScope.CurrentSheet.Possible values: • SearchScope.CurrentSheet – Searches only within the currently active worksheet.• SearchScope.EntireWorkbook – Searches across all worksheets in the workbook. |
| matchCase | bool (optional) | Specifies whether the search should be case-sensitive. If unspecified, the default is false (case-insensitive). |
| matchEntireCell | bool (optional) | Specifies whether the search text must match the entire cell content. If unspecified, the default is false (partial match allowed). |
The method returns an empty string array if any of the following conditions are met:
-
searchTextis null, empty, or contains only whitespace. -
AllowFindAndReplace is set to
false. - No matches are found according to the specified criteria.
- The workbook has not been initialized.
@page "/"
@using Syncfusion.Blazor.Spreadsheet
@using Syncfusion.Blazor.Buttons
<SfButton OnClick="FindAllMatches" Content="Find All"></SfButton>
<SfSpreadsheet @ref="SpreadsheetInstance" DataSource="DataSourceBytes">
<SpreadsheetRibbon></SpreadsheetRibbon>
</SfSpreadsheet>
@code {
public byte[] DataSourceBytes { get; set; }
public SfSpreadsheet SpreadsheetInstance;
protected override void OnInitialized()
{
string filePath = "wwwroot/Sample.xlsx";
DataSourceBytes = File.ReadAllBytes(filePath);
}
public async Task FindAllMatches()
{
// Retrieves all matching cell addresses in the active sheet.
string[] matches = await SpreadsheetInstance.FindAllAsync(
searchText: "Red",
searchScope: SearchScope.CurrentSheet,
matchCase: false,
matchEntireCell: false);
foreach (var address in matches)
{
Console.WriteLine($"Match found at: {address}");
}
}
}Replace
Replace is used to change the found contents of a cell within a sheet or workbook. Replace All is used to change all the matched contents of a cell within a sheet or workbook.
Replace via the UI
To replace data through the Ribbon UI, follow these steps:
- In the Home tab of the Ribbon, click the Search icon. The Find and Replace dialog opens.
- Enter the text to search for in the Find what box, and the replacement text in the Replace with box.
- Optionally, expand the dialog options and choose any of the following to refine the search:
| Option | Description |
|---|---|
| Search within | Search the target within the active Sheet (default) or in the entire Workbook. |
| Search by | Search either By Rows (default) or By Columns. |
| Match case | Find the matched value with case sensitivity. |
| Match exact cell contents | Find the exact matched cell value with entire cell match. |
- Choose one of the following actions
- Replace - Replaces the currently found occurrence with the replacement text and moves to the next match.
- Replace All - Replaces all matched occurrences with the replacement text.
Replace programmatically
The ReplaceAsync method searches for text matching the specified criteria and replaces either the first occurrence or all occurrences. The available parameters in the ReplaceAsync() method are:
| Parameter | Type | Description |
|---|---|---|
| searchText | string | Specifies the text to search for. This parameter is required and cannot be null, empty, or contain only whitespace. |
| replacementText | string | Specifies the text to replace the matched content with. If this parameter is null, it will be treated as an empty string. |
| searchScope | SearchScope (optional) | Specifies the scope of the search operation. Accepts values from the SearchScope enumeration. If unspecified, the default is SearchScope.CurrentSheet.Possible values: • SearchScope.CurrentSheet – Replaces matched values only within the currently active sheet.• SearchScope.EntireWorkbook – Replaces matched values across all worksheets in the workbook. |
| matchCase | bool (optional) | Specifies whether the search should be case-sensitive. If unspecified, the default is false (case-insensitive). |
| matchEntireCell | bool (optional) | Specifies whether the search text must match the entire cell content. If unspecified, the default is false (partial match allowed). |
| isReplaceAll | bool (optional) | Controls the replacement behavior. If unspecified, the default is false.Possible values: • false – Replaces only the first matched occurrence.• true – Replaces all matched occurrences in a single atomic operation. |
@page "/"
@using Syncfusion.Blazor.Spreadsheet
@using Syncfusion.Blazor.Buttons
<SfButton OnClick="ReplaceFirstMatch" Content="Replace"></SfButton>
<SfButton OnClick="ReplaceAllMatches" Content="Replace All"></SfButton>
<SfSpreadsheet @ref="SpreadsheetInstance" DataSource="DataSourceBytes">
<SpreadsheetRibbon></SpreadsheetRibbon>
</SfSpreadsheet>
@code {
public byte[] DataSourceBytes { get; set; }
public SfSpreadsheet SpreadsheetInstance;
protected override void OnInitialized()
{
string filePath = "wwwroot/Sample.xlsx";
DataSourceBytes = File.ReadAllBytes(filePath);
}
public async Task ReplaceFirstMatch()
{
// Replaces the first matched occurrence of "Sales" with "Revenue" in the active sheet.
await SpreadsheetInstance.ReplaceAsync("Sales", "Revenue");
}
public async Task ReplaceAllMatches()
{
// Replaces all matched occurrences of "Sales" with "Revenue" across the entire workbook.
await SpreadsheetInstance.ReplaceAsync("Sales", "Revenue",SearchScope.EntireWorkbook, false, false, true);
}
}Go to
Go To is used to navigate to a specific cell address in the sheet or workbook.
Go to programmatically
The GoTo method navigates to the specified cell or range and sets it as the active selection. If a sheet reference is included in the address, the method automatically switches to that sheet before navigating. The cellAddress parameter accepts various formats:
- Single cell reference (e.g.,
"Sheet1!A1","Sheet1!B5") — Navigates to the specified cell within the active sheet. - Cell range (e.g.,
"Sheet1!A1:C5") — Navigates to and selects the specified range within the active sheet.
@page "/"
@using Syncfusion.Blazor.Spreadsheet
@using Syncfusion.Blazor.Buttons
<SfButton OnClick="GoToCell" Content="Go To"></SfButton>
<SfSpreadsheet @ref="SpreadsheetInstance" DataSource="DataSourceBytes">
<SpreadsheetRibbon></SpreadsheetRibbon>
</SfSpreadsheet>
@code {
public byte[] DataSourceBytes { get; set; }
public SfSpreadsheet SpreadsheetInstance;
protected override void OnInitialized()
{
string filePath = "wwwroot/Sample.xlsx";
DataSourceBytes = File.ReadAllBytes(filePath);
}
public void GoToCell()
{
// Navigates to cell B12 within the active sheet.
SpreadsheetInstance.GoTo("Sheet1:A1");
}
}Limitations
- Replace All functionality is not restricted to selected range of cells.
- Find and Replace in Formulas not supported.