C#: Read or Remove Document Properties from Excel

Excel document properties, also known as metadata, are essential for understanding the content and context of an Excel file. They provide valuable information about the document's content, authorship, and creation/revision history, which can facilitate the efficient organization and retrieval of files. In addition to adding document properties to Excel, this article will show you how to read or remove document properties from Excel in C# 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

Read Standard and Custom Document Properties from Excel in C#

Excel properties are divided into two main categories:

  • Standard Properties: These are predefined properties that are built into Excel files. They typically include basic details about the file such as title, subject, author, keywords, etc.
  • Custom Properties: These are user-defined attributes that can be added to Excel to track additional information about the file based on your specific needs.

Spire.XLS for .NET allows to read both the standard and custom document properties of an Excel file. The following are the detailed steps:

  • Create a Workbook instance.
  • Load an Excel file using Workbook.LoadFromFile() method.
  • Create a StringBuilder instance.
  • Get a collection of all standard document properties using Workbook.DocumentProperties property.
  • Get specific standard document properties using the properties of the BuiltInDocumentProperties class and append them to the StringBuilder instance.
  • Get a collection of all custom document properties using Workbook.CustomDocumentProperties property.
  • Iterate through the collection.
  • Get the name and value of each custom document property using IDocumentProperty.Name and IDocumentProperty.Value properties and append them to the StringBuilder instance.
  • Write the content of the StringBuilder instance into a txt file.
  • C#
using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core;
using System.IO;
using System.Text;

namespace GetExcelProperties
{
    class Program
    {

        static void Main(string[] args)
        {
            {
                //Create a Workbook instance
                Workbook workbook = new Workbook();

                //Load a sample Excel file
                workbook.LoadFromFile("Budget Template.xlsx");

                //Create a StringBuilder instance
                StringBuilder sb = new StringBuilder();

                //Get a collection of all standard document properties
                BuiltInDocumentProperties standardProperties = workbook.DocumentProperties;

                //Get specific standard properties and append them to the StringBuilder instance
                sb.AppendLine("Standard Document Properties:");
                sb.AppendLine("Title: " + standardProperties.Title);
                sb.AppendLine("Subject: " + standardProperties.Subject);
                sb.AppendLine("Category: " + standardProperties.Category);
                sb.AppendLine("Keywords: " + standardProperties.Keywords);
                sb.AppendLine("Comments: " + standardProperties.Comments);
                sb.AppendLine();

                //Get a collection of all custom document properties
                ICustomDocumentProperties customProperties = workbook.CustomDocumentProperties;

                sb.AppendLine("Custom Document Properties:");
                //Iterate through the collection
                for (int i = 0; i < customProperties.Count; i++)
                {
                    //Get the name and value of each custom document property and append them to the StringBuilder instance
                    string name = customProperties[i].Name;
                    string value = customProperties[i].Value.ToString();
                    sb.AppendLine(name + ": " + value);
                }

                //Write the content of the StringBuilder instance into a text file
                File.WriteAllText("GetExcelProperties.txt", sb.ToString());            
            }
        }
    }
}

C#: Read or Remove Document Properties from Excel

Remove Standard and Custom Document Properties from Excel in C#

You can easily delete standard document properties from an Excel file by setting their values as empty. For custom document properties, you can use the ICustomDocumentProperties.Remove() method to delete them. The following are the detailed steps:

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a collection of all standard document properties using Workbook.DocumentProperties property.
  • Set the values of specific standard document properties as empty through the corresponding properties of the BuiltInDocumentProperties class.
  • Get a collection of all custom document properties using Workbook.CustomDocumentProperties property.
  • Iterate through the collection.
  • Delete each custom property from the collection by its name using ICustomDocumentProperties.Remove(string strName) method.
  • Save the result file using Workbook.SaveToFile() method.
  • C#
using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core;

namespace DeleteExcelProperties
{
    class Program
    {

        static void Main(string[] args)
        {
            {
                //Create a Workbook instance
                Workbook workbook = new Workbook();

                //Load a sample Excel file
                workbook.LoadFromFile("Budget Template.xlsx");

                //Get a collection of all standard document properties
                BuiltInDocumentProperties standardProperties = workbook.DocumentProperties;

                //Set the value of each standard document property as empty
                standardProperties.Title = "";
                standardProperties.Subject = "";
                standardProperties.Category = "";
                standardProperties.Keywords = "";
                standardProperties.Comments = "";
 
                //Get a collection of all custom document properties
                ICustomDocumentProperties customProperties = workbook.CustomDocumentProperties;

                //Iterate through the collection
                for (int i = customProperties.Count -1; i >=0; i--)
                {
                    //Delete each custom document property from the collection by its name
                    customProperties.Remove(customProperties[i].Name);

                }

                //Save the result file
                workbook.SaveToFile("DeleteDocumentProperties.xlsx", ExcelVersion.Version2016);            
            }
        }
    }
}

C#: Read or Remove Document Properties from 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.