管理 Microsoft® Excel 檔案元資料 via .NET

使用伺服器端 .NET API 檢視、新增、更新、刪除或擷取內建和自訂 Excel 檔案屬性。

 

.NET Excel API 支援管理系統定義(內建)屬性,如標題、作者姓名、文件統計等,以及使用者定義(自訂)屬性(以名稱-值對的形式)。有 作業本類 載入文件,以及 工作表集合 處理工作表的收集以及 工作表類 用於表示單一工作表。與這些類別一起,BuiltInDocumentProperties、CustomDocumentProperties 讓元資料管理流程變得簡單。

管理內建屬性

為了管理系統定義的屬性,API 提供 內建文件屬性 ,程式設計師可以輕鬆存取內建屬性並更新其值。根據應用程式的要求,開發人員可以使用索引或屬性名稱 文檔屬性集合 .

C# 管理內建屬性的程式碼
//Create workbook object.
Workbook wb = new Workbook();
//Access system defined document property collection.
Aspose.Cells.Properties.BuiltInDocumentPropertyCollection sdmd = wb.BuiltInDocumentProperties;
//Set the language of the Excel document.
sdmd.Language = "German, French";
//Save the workbook in xlsx format.
wb.Save(outputDir + "system-defined-properties-updated.xlsx", SaveFormat.Xlsx);
 

管理自訂屬性

為了管理使用者定義的屬性,API 提供 自訂文件屬性 ,開發人員可以輕鬆存取已新增的屬性以及新增屬性。為了新增自訂屬性, 添加方法自訂文件屬性集合 類別添加屬性並傳回新屬性的引用作為 屬性.文檔屬性 目的。 DocumentProperty 類別用於擷取文件屬性的名稱、值和類型,如下所示 文檔屬性.名稱 , 文檔屬性.值 , 文檔屬性.類型 返回其中之一 財產種類 枚舉值。

C# 在 Excel 檔案中新增元資料的程式碼
// string dataDir = "he path to the documents directory."
// Instantiate a Workbook object
// Open an Excel file
Workbook wkb = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = wkb.Worksheets.CustomDocumentProperties;
// Adding a custom document property to the Excel file
Aspose.Cells.Properties.DocumentProperty publisher = customProperties.Add("Publisher", "Aspose");
// Add link to content.
customProperties.AddLinkToContent("Owner", "MyRange");
// way to access custom property by using the property name
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["Owner"];
// Saving resultant spreadsheet
wkb.Save(dataDir + "out_sample-document-properties.xlsx");
C# 刪除 Excel 檔案中的自訂屬性的程式碼
//string dataDir = "The path to the documents directory";
// Instantiate a Workbook object
// Open an Excel file
Workbook wkb = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = wkb.Worksheets.CustomDocumentProperties;
// Removing a custom document property
customProperties.Remove("Publisher");
// Save the file
wkb.Save(dataDir + "out_sample-document-properties.xlsx");