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

  • 2019 年 10 月 21 日
  • 筆記

在文檔屬性中,可以設置諸多關於文檔的資訊,如創建時間、作者、單位、類別、關鍵詞、備註等摘要資訊以及一些自定義的文檔屬性。下面將通過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);*/          }      }  }

文檔屬性刪除結果:

 

(本文完)