Find and Replace

  • 4 min to read

The SpreadsheetControl allows you to search for specific data in a document. You can perform a search using the SpreadsheetControl's user interface or directly in code using the corresponding Search method overloads.

Search Using the Find and Replace Dialog

You can find data in the current worksheet using the Find and Replace feature of the SpreadsheetControl. To perform a search, on the Home tab, in the Editing group, click the Find & Select button. The button's drop-down menu will be displayed.

FindAndSelectButton

Next, do one of the following.

  • Click Find in the Find & Select drop-down menu (or press CTRL+F) to perform a search in the active worksheet. The Find and Replace dialog (with the Find tab activated) will be invoked.

    DXSpreadsheet_FindDialog

    In the Find what field, enter the text or number you wish to find, and click the Find Next button to start the search. To define the direction of the search, in the Search field, select the By Rows or By Columns drop-down item. In the Look in field, select Values (to search cell values only) or Formulas (to search cell values and formula expressions, excluding the calculated results). To perform a case-sensitive search, select the Match Case check box. To restrict the search to the entire cell content, select the Match entire cell contents check box.

  • Click Replace in the Find & Select drop-down menu (or press CTRL+H) to search for a text string and optionally replace it with another value. The Find and Replace dialog (with the Replace tab activated) will be invoked.

    DXSpreadsheet_ReplaceDialog

    In the Find what field, enter the text or number you wish to find. In the Replace with field, enter the replacement text for your search term. Click the Replace button to replace only the value of the selected matching cell, or the Replace All button to replace all occurrences of the search term. Note that the Replace tab provides the same search options as the Find tab, with one exception: you can only select the Formulas drop-down item in the Look in box, so only the underlying formulas (not the calculated results) will be examined when searching for matches to your search term.

Search in Code

The SpreadsheetControl also allows you to find text in a range, worksheet or entire document programmatically using the CellRange.Search, Worksheet.Search or IWorkbook.Search methods, respectively. To set options affecting search in a document, create an instance of the SearchOptions class and pass it as a parameter to the Search method. As in the case of the user interface you can set the following advanced options.

The example below demonstrates how to perform a search with the specified options in the active worksheet and highlight all matching cells.

workbook.LoadDocument("Documents\ExpenseReport.xlsx")
workbook.Calculate()
Dim worksheet As Worksheet = workbook.Worksheets(0)

' Specify the search term.
Dim searchString As String = Date.Today.ToString("d")

' Specify search options.
Dim options As New SearchOptions()
options.SearchBy = SearchBy.Columns
options.SearchIn = SearchIn.Values
options.MatchEntireCellContents = True

' Find all cells containing today's date and paint them light-green.
Dim searchResult As IEnumerable(Of Cell) = worksheet.Search(searchString, options)
For Each cell As Cell In searchResult
    cell.Fill.BackgroundColor = System.Drawing.Color.LightGreen
Next cell

The image below shows the result of executing the code. Today's date is located in the expense report and highlighted in light-green.

DXSpreadsheet_SearchResult