Add custom properties to Excel document using XlsIO
Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Using this library, you can add custom properties in Excel document in C# and VB.NET.
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 in Excel document programmatically:
Step 1: Create a new C# console application project.
Step 2: Install the Syncfusion.XlsIO.WinForms NuGet package as 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 4 worksheets workbook = application.Workbooks.Create(4); #region CustomProperites //Add custom properties to 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 4 worksheets workbook = application.Workbooks.Create(4) 'Add custom properties to 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("Outpur.xlsx") workbook.SaveAs(stream) End Using
A complete working example to add custom properties into 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.