C# 添加、讀取、刪除Excel文檔屬性

在文檔屬性中,可以設置諸多關於文檔的信息,如創建時間、作者、單位、類別、關鍵詞、備註等摘要信息以及一些自定義的文檔屬性。下面將通過C#程序來演示如何設置,同時對文檔內的已有信息,也可以實現讀取或刪除等操作。

示例大綱:

1. 添加文檔屬性

  1.1 添加摘要信息

  1.2 添加自定義文檔信息

2. 讀取文檔屬性

3. 刪除文檔信息

  3.1 刪除所有摘要信息、自定義文檔屬性

  3.2 刪除指定只要信息、自定義文檔屬性

 

使用工具:Spire.XLS for .NET pack

獲取方法1:通過官網下載包。下載後,解壓文件,安裝Bin文件夾下的程序。安裝後,將安裝路徑下Bin文件夾下的Spire.Xls.dll文件添加引用至vs項目程序。如下所示:

獲取方法2可通過Nuget下載。

 

C# 示例

【示例】添加文檔屬性

using Spire.Xls;
using System;

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

            //設置摘要
            workbook.DocumentProperties.Author = "Mara";
            workbook.DocumentProperties.Title = "摘要";
            workbook.DocumentProperties.Keywords = "摘要,屬性";
            workbook.DocumentProperties.Category = "展示文檔";
            workbook.DocumentProperties.Company = "冰藍科技";
            workbook.DocumentProperties.Comments = "請勿修改";
            workbook.DocumentProperties.Subject = "測試";
            workbook.DocumentProperties.Manager = "Tom";


            //設置自定義屬性
            workbook.CustomDocumentProperties.Add("_MarkAsFinal", true);
            workbook.CustomDocumentProperties.Add("聯繫電話", 81705109);
            workbook.CustomDocumentProperties.Add("更新時間", DateTime.Now);

            //保存文檔
            workbook.SaveToFile("AddProperties.xlsx", FileFormat.Version2010);
        }
    }
}

文檔屬性添加效果:

【示例2】讀取文檔信息

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

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

            //獲取文檔屬性
            Console.WriteLine("摘要信息:");
            Console.WriteLine("標題: " + wb.DocumentProperties.Title);
            Console.WriteLine("主題: " + wb.DocumentProperties.Subject);
            Console.WriteLine("作者: " + wb.DocumentProperties.Author);
            Console.WriteLine("管理者: " + wb.DocumentProperties.Manager);
            Console.WriteLine("公司: " + wb.DocumentProperties.Company);
            Console.WriteLine("類別: " + wb.DocumentProperties.Category);
            Console.WriteLine("關鍵字: " + wb.DocumentProperties.Keywords);
            Console.WriteLine("備註: " + wb.DocumentProperties.Comments);

            //獲取自定義屬性
            Console.WriteLine("\n自定義屬性:");
            for (int i = 0; i < wb.CustomDocumentProperties.Count; i++)
            {
                Console.WriteLine(wb.CustomDocumentProperties[i].Name + ": " + wb.CustomDocumentProperties[i].Value);
            }
            Console.Read();
        }
    }
}

文檔屬性讀取結果:

 

【示例3】刪除文檔屬性

using Spire.Xls;

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

            //刪除摘要及自定義文檔屬性
            workbook.DocumentProperties.Clear();//刪除所有摘要信息
            workbook.CustomDocumentProperties.Clear();//刪除所有自定義文檔屬性
            
            //保存文檔
            workbook.SaveToFile("DeleteProperties.xlsx", FileFormat.Version2013);

            /*//刪除指定摘要及自定義文檔屬性
            workbook.DocumentProperties.Author = "";//設置指定摘要信息爲空,刪除摘要內容
            workbook.CustomDocumentProperties.Remove("聯繫電話");//刪除指定名稱的自定義文檔屬性
            workbook.SaveToFile("DeleteCustomDocumentProperties.xlsx", FileFormat.Version2013);*/
        }
    }
}

文檔屬性刪除結果:

 

(本文完)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章