How to validate an email with data validation in .NET Core Excel?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
Using Syncfusion, we can work with data validation. In this article, we will explore how to validate an email address with data validation.
Steps to validate an email address with data validation programmatically:
Step 1: Create a new C# console application.
Step 2: Install the Syncfusion.XlsIO.Net.Core NuGet package as reference to your .NET Core application from NuGet.org.
Step 3: Include the following namespaces in Program.cs file.
C#
using Syncfusion.XlsIO;
Step 4: Include the following code snippet to validate an email address with data validation.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream("../../../Data/InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];
//Data validation for email format
IDataValidation validation = worksheet.Range["G2:G7"].DataValidation;
validation.AllowType = ExcelDataType.Formula;
validation.FirstFormula = "=AND(ISNUMBER(SEARCH(\"@\", G2:G7)), ISNUMBER(SEARCH(\".\", G2:G7, SEARCH(\"@\", G2:G7))))";
//Shows the error message
validation.ErrorBoxText = "Please enter a valid Email address.";
validation.ErrorBoxTitle = "Invalid Email Format";
validation.PromptBoxText = "Enter an Email address";
validation.IsPromptBoxVisible = true;
validation.ShowPromptBox = true;
//Saving the workbook as stream
FileStream OutputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(OutputStream);
//Dispose stream
inputStream.Dispose();
OutputStream.Dispose();
}
Download a complete working sample demonstrating how to validate an email address with data validation using C# from here.
If you enter an email address that does not satisfy the email format, an error message will pop up as configured.
Below is the output file for email validation using data validation.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheets or workbooks, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to a worksheet using Template Markers, and most importantly PDF and Image conversions with code examples.
Refer here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to validate an email address with data validation in .NET Core Excel using C#.
You can refer to our ASP.NET Core XIsIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core XIsIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!