The Excel spreadsheets have minimized the efforts required to keep the huge data and perform complex calculations automatically. Alongside, Finding and replacing text in Excel files is one of the frequently performed operations in Excel file. Therefore, in this article, I will show you how to find and replace text in Excel files programmatically in C# with a few lines of code.
- Find and replace text in Excel Workbook in C#
- Find and replace text in Excel Workbook using regular expression (Regex) in C#
C# API to Find and Replace Text in Excel
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 in C# 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#.
C# Replace Text in Excel Sheets with Regular Expression
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 in C#.
Use Aspose.Cell for .NET for Free
Get a free temporary license of Aspose.Cells for .NET and enjoy its powerful features.
Conclusion
Finding and replacing text in Excel sheets is a common task in data processing. With Aspose.Cells for .NET, you can easily achieve this in your C# applications. In this blog post, we’ve shown you how to get started and customize the text replacement options in a .NET application. We have also covered how to use regular expressions to find and replace text in an Excel sheet. Aspose.Cells is a powerful library that can help you streamline your Excel automation tasks and improve your workflow.