Find and Replace in Excel using C#

This article guides on how to find and replace in Excel using C#. It has details to set the IDE, a list of steps, and a sample code to replace a word in Excel using C#. Various options are explored in this article to customize the search method and criteria while searching text in the source Excel file.

Steps to Find and Change in Excel using C#

  1. Set the IDE to use Aspose.Cells for .NET to find and replace words
  2. Load the source XLSX file into the Workbook class object
  3. Create the FindOptions class object for setting the search options
  4. Iterate through all the worksheets in the workbook
  5. Use the Find() method in the cells collection of the current sheet
  6. If a cell with the text is found, use the Replace() method with the target text and the text to be replaced
  7. Set the new value in the cell after replacing the target string in the cell’s content
  8. Parse through all the cells and finally save the resultant workbook

These steps define the process to find and replace multiple values in Excel using C#. Load the source Excel file into the Workbook, create the FindOptions object for setting look in type and look at type, parse through all the worksheets in the workbook, and process all the cells in each worksheet. Find text in each cell and, if found, replace it with new text; finally, save the resultant workbook with the replaced text.

Code to Find and Replace Text in Excel using C#

The above code has demonstrated the process to replace data in Excel using C#. The FindOptions supports setting the case sensitivity during search, specifying how to match the string, defining where to search, and deciding whether the search string is a literal or regular expression. You can also search backward or from the start according to your requirements.

This article has taught us the process to search and replace text in an Excel file. To decrypt an Excel file, refer to the article Decrypt Excel File in C#.