How to Add Custom Properties to WinForms Excel Document?
Syncfusion Essential® XlsIO is a .NET Excel library used to create, read, and edit to Excel document. Using this library, you can add custom properties in Excel document in C# and VB.NET in WinForms Excel.
Document properties, also known as metadata, are details about a file that describe or identify it. There are four types of document properties, namely:
- Standard properties
- Automatically updated properties
- Custom properties
- Document library properties
Custom document properties can be selected from the list of names or can be defined on your own. You can select type of property as date, text, number or yes or no and assign a value.
Steps to add custom properties to an Excel document programmatically:
Step 1: Create a new C# console application project.
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
Step 3: Include the following namespace in the program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
Step 4: Use the following code snippet to add custom properties in Excel file.
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { // Instantiate the Excel application object IApplication application = excelEngine.Excel; // The workbook is opened IWorkbook workbook; // Creating a workbook with 4 worksheets workbook = application.Workbooks.Create(4); #region CustomProperties // Add custom properties to the workbook workbook.CustomDocumentProperties["Department"].Text = "XlsIO"; workbook.CustomDocumentProperties["Module"].Text = "Shapes"; workbook.CustomDocumentProperties["Status"].Text = "Completed"; workbook.CustomDocumentProperties["Worksheets_Count"].Value = workbook.Worksheets.Count; #endregion // Save the workbook with added custom properties and close Stream stream = File.Create("Output.xlsx"); workbook.SaveAs(stream); }
VB.NET
Using excelEngine As ExcelEngine = New ExcelEngine 'Instantiate the excel application object Dim application As IApplication = excelEngine.Excel 'The workbook is opened Dim workbook As IWorkbook 'Creating a workbook with 4 worksheets workbook = application.Workbooks.Create(4) 'Add custom properties to the workbook workbook.CustomDocumentProperties("Department").Text = "XlsIO" workbook.CustomDocumentProperties("Module").Text = "Shapes" workbook.CustomDocumentProperties("Status").Text = "Completed" workbook.CustomDocumentProperties("Worksheets_Count").Value = workbook.Worksheets.Count 'Save the workbook with added custom properties and close Dim stream As Stream = File.Create("Output.xlsx") workbook.SaveAs(stream) End Using
A complete working example to add custom properties to an Excel document can be downloaded from Add-Custom-properties-in-Excel-document.zip.
By executing the program, you will get the output as follows.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to add custom properties to WinForms Excel document.
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.
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!