Find and Replace
The document model offers a convenient way to find and replace text and numbers in a workbook, worksheet, or a specified range of cells.
Find and FindAll
Both Workbook and Worksheet classes expose methods that search their contents for a specific value. The two classes offer a Find() method that returns the first occurrence of the sought string and a FindAll() method that returns all occurrences of the specified value. Both methods require a single parameter of type FindOptions that determines how the search is performed. The following list describes the properties of the FindOptions class:
-
FindWhat: Specifies the sought string. -
FindWithin: Determines if the search covers the wholeWorkbookor a particularWorksheet. If you call theFind()method of theWorkbookclass and theFindWithinoption is set toWorkbook, the search covers the entire workbook. If the option isWorksheet, the search covers only the active worksheet. If you call theFind()method of theWorksheetclass, theFindWithinproperty is ignored and the search covers the worksheet instance that invokes it. -
FindBy: Indicates whether the search is performed by rows or by columns. -
FindIn: Indicates if the search includes formulas or only result values. -
MatchCase: Determines if the search must match the casing of the sought string. -
MatchEntireCellContents: Indicates whether the sought string must match the entire cell content. -
StartCell: Marks the cell from which the search begins. -
SearchRanges: If the property is set tonull, the search covers the entire workbook or worksheet, depending on theFindWithinproperty. If ranges are defined, the search covers only those ranges of the active sheet. This property applies only to theFind()andReplace()methods and is disregarded in theFindAll()andReplaceAll()methods.
Example 1 creates a new workbook with two empty worksheets and assigns sample values to the sheets. The FindOptions instance specifies that the search covers the whole workbook and starts from cell A1 of the first worksheet. The snippet shows how to use the Find() and FindAll() methods.
Example 1: Perform Find and Find All
Workbook workbook = new Workbook();
Worksheet worksheet1 = workbook.Worksheets.Add();
Worksheet worksheet2 = workbook.Worksheets.Add();
worksheet1.Cells[1, 1].SetValue("SUMMARY");
worksheet1.Cells[1, 2].SetValue("=SUM(5, 6)");
worksheet2.Cells[2, 2].SetValue("=SUM(4, 4)");
worksheet2.Cells[2, 3].SetValue("SUM");
FindOptions options = new FindOptions()
{
StartCell = new WorksheetCellIndex(worksheet1, 0, 0),
FindBy = FindBy.Rows,
FindIn = FindInContentType.Formulas,
FindWhat = "SUM",
FindWithin = FindWithin.Workbook,
};
FindResult findResult = workbook.Find(options);
IEnumerable<FindResult> findResults = workbook.FindAll(options);
Replace and ReplaceAll
In addition to the Find() and FindAll() methods, the Workbook and Worksheet classes offer two more methods that allow you to alter the found strings: Replace() and ReplaceAll(). The Replace() method replaces the string of the first occurrence while ReplaceAll() alters all encountered occurrences. The two methods take one argument of type ReplaceOptions that specifies how the search is performed and the string that replaces the occurrences. The class derives from FindOptions and defines one more property:
ReplaceWith: Specifies the string that replaces any found value.
Example 2 creates a workbook from scratch with two empty worksheets and adds sample values. The ReplaceOptions instance specifies that the replace covers the whole workbook and includes formula values. The operation starts from cell A1 of the first worksheet and the search proceeds by columns.
Example 2: Perform Replace and Replace All
Workbook workbook = new Workbook();
Worksheet worksheet1 = workbook.Worksheets.Add();
Worksheet worksheet2 = workbook.Worksheets.Add();
worksheet1.Cells[1, 1].SetValue("SUMMARY");
worksheet1.Cells[1, 2].SetValue("=SUM(5, 6)");
worksheet2.Cells[2, 2].SetValue("=SUM(4, 4)");
worksheet2.Cells[2, 3].SetValue("SUM");
ReplaceOptions options = new ReplaceOptions()
{
StartCell = new WorksheetCellIndex(worksheet1, 0, 0),
FindBy = FindBy.Rows,
FindIn = FindInContentType.Formulas,
FindWhat = "SUM",
ReplaceWith = "Test",
FindWithin = FindWithin.Workbook,
};
FindResult findResult = workbook.Find(options);
options.StartCell = findResult.FoundCell;
if (workbook.Replace(options))
{
Console.WriteLine("Replace was successful!");
}
workbook.ReplaceAll(options);