C ා add, read and delete Excel document properties

Posted by alexk1781 on Mon, 21 Oct 2019 19:53:13 +0200

In document properties, you can set many information about the document, such as creation time, author, company, category, keywords, notes and other summary information, as well as some custom document properties. The following will demonstrate how to set it through the C ා program. At the same time, the existing information in the document can also be read or deleted.

Sample Outline:

1. Add document properties

1.1 add summary information

1.2 add custom document information

2. Read document properties

3. Delete document information

3.1 delete all summary information and custom document properties

3.2 delete specified information and custom document properties

 

Using tool: fire.xls for. Net pack

Access method 1: through the official website download Bag. After downloading, extract the file and install the program under Bin folder. After installation, add the Spire.Xls.dll file under the Bin folder in the installation path to the vs project program. As follows:

 

 

Access method 2: available through Nuget Download.

 

C# example

[example] add document attribute

using Spire.Xls;
using System;

namespace AddProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Excel File
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("test.xlsx");

            //Summary of settings
            workbook.DocumentProperties.Author = "Mara";
            workbook.DocumentProperties.Title = "abstract";
            workbook.DocumentProperties.Keywords = "Summary, properties";
            workbook.DocumentProperties.Category = "Display document";
            workbook.DocumentProperties.Company = "Ice blue technology";
            workbook.DocumentProperties.Comments = "Do not modify";
            workbook.DocumentProperties.Subject = "test";
            workbook.DocumentProperties.Manager = "Tom";


            //Set custom properties
            workbook.CustomDocumentProperties.Add("_MarkAsFinal", true);
            workbook.CustomDocumentProperties.Add("Contact number", 81705109);
            workbook.CustomDocumentProperties.Add("Update time", DateTime.Now);

            //Save document
            workbook.SaveToFile("AddProperties.xlsx", FileFormat.Version2010);
        }
    }
}

Document property adding effect:

 

[example 2] read document information

using Spire.Xls;
using Spire.Xls.Collections;
using Spire.Xls.Core;
using System;

namespace ReadProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Excel File
            Workbook wb = new Workbook();
            wb.LoadFromFile("AddProperties.xlsx");

            //Get document properties
            Console.WriteLine("Summary information:");
            Console.WriteLine("Title: " + wb.DocumentProperties.Title);
            Console.WriteLine("theme: " + wb.DocumentProperties.Subject);
            Console.WriteLine("author: " + wb.DocumentProperties.Author);
            Console.WriteLine("Controller: " + wb.DocumentProperties.Manager);
            Console.WriteLine("company: " + wb.DocumentProperties.Company);
            Console.WriteLine("category: " + wb.DocumentProperties.Category);
            Console.WriteLine("Keyword: " + wb.DocumentProperties.Keywords);
            Console.WriteLine("Remarks: " + wb.DocumentProperties.Comments);

            //Get custom properties
            Console.WriteLine("\n Custom properties:");
            for (int i = 0; i < wb.CustomDocumentProperties.Count; i++)
            {
                Console.WriteLine(wb.CustomDocumentProperties[i].Name + ": " + wb.CustomDocumentProperties[i].Value);
            }
            Console.Read();
        }
    }
}

Document property read result:

 

 

[example 3] delete document attribute

using Spire.Xls;

namespace DeleteProperties
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load Workbook
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("AddProperties.xlsx");

            //Delete summary and custom document properties
            workbook.DocumentProperties.Clear();//Delete all summary information
            workbook.CustomDocumentProperties.Clear();//Delete all custom document properties
            
            //Save document
            workbook.SaveToFile("DeleteProperties.xlsx", FileFormat.Version2013);

            /*//Delete specified summary and custom document properties
            workbook.DocumentProperties.Author = "";//Set the specified summary information to be empty and delete the summary content
            workbook.CustomDocumentProperties.Remove("Contact number "); / / delete the custom document property with the specified name
            workbook.SaveToFile("DeleteCustomDocumentProperties.xlsx", FileFormat.Version2013);*/
        }
    }
}

Document property deletion result:

 

(end of this paper)

Topics: C# Attribute Excel