The Excel spreadsheets have minimized the efforts required to keep the huge data and perform complex calculations automatically. Excel automation was the next big achievement that has streamlined the way of creating Excel spreadsheets and performing a variety of operations on the data programmatically.
Finding and replacing text in Excel is one of the frequently performed operations in Excel automation. Therefore, in this article, I will show you how to find and replace text in large Excel spreadsheets programmatically in C# with a few lines of code. Ultimately, you will learn how to:
- Find and replace text in Excel Workbook in C#
- Find and replace text in Excel Workbook using regular expression (Regex) in C#
API for Finding and Replacing Text in Excel Workbooks
Aspose.Cells for .NET is a powerful API that allows you to completely automate the process of creating, editing, parsing and converting the Excel spreadsheet programmatically. The API also provides easy ways of finding and replacing text based on the plain text as well as regular expressions (regex). You can either download the API or install it in your application via NuGet Package Manager or Package Manager Console.
NuGet Package Manager
Package Manager Console
PM> Install-Package Aspose.Cells
Find and Replace Text in Excel using C#
The following is the simple recipe to find and replace text in an Excel workbook using Aspose.Cells for .NET.
- Create an instance of Workbook class and initialize it with the path of the Excel file.
- Create an instance of ReplaceOptions class and set options such as case sensitivity.
- Call Workbook.Replace(String, String, ReplaceOptions) method to find and replace the text.
- Save the updated Excel file using Workbook.Save(String) method.
The following code sample shows how to find and replace text in Excel using C#.
Find and Replace Text with Regular Expression in Excel using C#
You can also find and replace text that matches a particular pattern, such as Email IDs, SSN, etc. In order to do that, just set the ReplaceOptions.RegexKey property to true and call Workbook.Replace(String, String, ReplaceOptions) method.
The following code sample shows how to find and replace the text with regex in the Excel workbook.
Try Aspose.Cell for .NET for Free
Get a free temporary license of Aspose.Cells for .NET and enjoy its powerful features.