How to apply URL format validation in Excel using C#, VB.NET?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.
This article explains how to apply URL format validation in Excel using
Include the following namespace in the Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Use the following code snippet for applying URL format validation in Excel.
C#
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Data validation for URL format in A3 cell
IDataValidation validation = worksheet.Range["A3"].DataValidation;
validation.AllowType = ExcelDataType.Formula;
validation.FirstFormula = "=AND(ISNUMBER(SEARCH(\"://\", A3)), ISNUMBER(SEARCH(\".\", A3, SEARCH(\"://\", A3)+3)), ISERROR(SEARCH(\" \", A3)))";
//Shows the error message
validation.ShowErrorBox = true;
validation.ErrorBoxText = "Please enter a valid URL.";
validation.ErrorBoxTitle = "Invalid URL Format";
validation.PromptBoxText = "Data validation for URL";
validation.ShowPromptBox = true;
//Save the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
//Dispose streams
outputStream.Dispose();
}
VB.NET
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Data validation for URL format in A3 cell
Dim validation As IDataValidation = worksheet.Range("A3").DataValidation
validation.AllowType = ExcelDataType.Formula
validation.FirstFormula = "=AND(ISNUMBER(SEARCH(""://"", A3)), ISNUMBER(SEARCH(""."", A3, SEARCH(""://"", A3)+3)), ISERROR(SEARCH("" "", A3)))"
'Shows the error message
validation.ShowErrorBox = True
validation.ErrorBoxText = "Please enter a valid URL."
validation.ErrorBoxTitle = "Invalid URL Format"
validation.PromptBoxText = "Data validation for URL"
validation.ShowPromptBox = True
'Save the workbook
workbook.SaveAs("Output.xlsx")
End Using
You can get the complete sample for applying URL format validation in Excel from
I hope you enjoyed learning about how to apply URL format validation in Excel using
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!