C#/VB.NET: Add Document Properties in Excel

2023-02-15 05:20:00 Written by  support iceblue
Rate this item
(0 votes)

Document properties, also known as metadata, are a set of data that describe a document. In Excel, you can add built-in document properties such as author, title, and keywords to quickly locate and identify documents in a folder. Or you can also add custom properties to provide more information about the Excel document. In this article, you will learn how to programmatically add built-in and custom document properties to an Excel document using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Add Built-in Document Properties in Excel in C# and VB.NET

Built-in document properties are basic information about a document such as title, subject, author, category, etc. The names of these properties are predefined that cannot be edited, but Spire.XLS for .NET allows you to set specific values for these properties. The following are the detailed steps.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.LoadFromFile() method.
  • Get the built-in document properties of the document using Workbook.DocumentProperties property.
  • Set specific document properties such as title, author, keywords and comments using the properties of BuiltInDocumentProperties class.
  • Save the result document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ExcelProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("sample.xlsx");

            //Set built-in document properties for the Excel workbook
            workbook.DocumentProperties.Author = "E-iceblue Team";
            workbook.DocumentProperties.Title = "Add Built-in Document Properties in Excel ";
            workbook.DocumentProperties.Keywords = "Excel, Document Properties, C#, VB.NET";
            workbook.DocumentProperties.Category = "Spire.XLS Demo";
            workbook.DocumentProperties.Company = "E-iceblue";
            workbook.DocumentProperties.Comments = "Document properties are details about a file that describe or identify it.";

            //Save the result document
            workbook.SaveToFile("ExcelProperties.xlsx", FileFormat.Version2013);
        }
    }
}

C#/VB.NET: Add Document Properties in Excel

Add Custom Document Properties in Excel in C# and VB.NET

Custom document properties are additional properties that you can define for an Excel document. Spire.XLS for .NET allows you to add custom properties with specified names and values using ICustomDocumentProperties.Add() method. The following are the detailed steps.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.LoadFromFile() method.
  • Get the custom document properties of the document using Workbook.CustomDocumentProperties property.
  • Add custom document properties with different data types to the document using ICustomDocumentProperties.Add() method.
  • Save the result document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;
using System;

namespace CustomExcelProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("sample.xlsx");

            //Add custom document properties to the document
            workbook.CustomDocumentProperties.Add("_MarkAsFinal", true);
            workbook.CustomDocumentProperties.Add("The Editor", "E-iceblue");
            workbook.CustomDocumentProperties.Add("Phone Number", 12345678);
            workbook.CustomDocumentProperties.Add("Document ID", 1);
            workbook.CustomDocumentProperties.Add("Revision Date", DateTime.Now);

            //Save the result document
            workbook.SaveToFile("ExcelCustomProperties.xlsx", FileFormat.Version2013);
        }
    }
}

C#/VB.NET: Add Document Properties in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Wednesday, 15 February 2023 01:38