管理 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"); |