管理文档属性

介绍

Microsoft Excel提供了向电子表格文件添加属性的功能。这些文档属性提供有用信息,分为以下2类。

  • 系统定义(内置)属性:内置属性包含有关文档的一般信息,如文档标题、作者姓名、文档统计信息等。
  • 用户定义(自定义)属性:最终用户以名称-值对的形式定义的自定义属性。

如何使用Microsoft Excel管理文档属性

Microsoft Excel允许您以所见即所得的方式管理Excel文件的文档属性。请按照以下步骤在Excel 2016中打开属性对话框。

  1. 文件菜单中选择信息
选择信息菜单
todo:image_alt_text
  1. 点击属性标题并选择"高级属性"。
单击高级属性选择
todo:image_alt_text
  1. 管理文件的文档属性。
属性对话框
todo:image_alt_text
在属性对话框中,有不同的选项卡,如常规、摘要、统计、内容和自定义。每个选项卡都可以帮助配置文件相关的不同信息。自定义选项卡用于管理自定义属性。

如何使用Aspose.Cells处理文档属性

开发人员可以使用Aspose.Cells API动态管理文档属性。此功能帮助开发人员存储有用信息,如文件接收时间、处理时间戳等。

如何访问文档属性

Aspose.Cells API支持内建和自定义文档属性。Aspose.Cells的Workbook类代表Excel文件,类似于Excel文件,Workbook类可以包含多个工作表,每个工作表由Worksheet类表示,而工作表的集合由WorksheetCollection类表示。

使用WorksheetCollection如下访问文件的文档属性。

WorksheetCollection.BuiltInDocumentPropertiesWorksheetCollection.CustomDocumentProperties都返回Aspose.Cells.Properties.DocumentPropertyCollection的实例。此集合包含Aspose.Cells.Properties.DocumentProperty对象,每个对象代表一个单独的内建或自定义文档属性。

应用程序要求如何访问属性取决于;即通过从DocumentPropertyCollection中的索引或属性名称来访问属性,如下面的示例所示。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Accessing a custom document property by using the property name
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["ContentTypeId"];
Console.WriteLine(customProperty1.Name + " " + customProperty1.Value);
// Accessing the same custom document property by using the property index
Aspose.Cells.Properties.DocumentProperty customProperty2 = customProperties[0];
Console.WriteLine(customProperty2.Name + " " + customProperty2.Value);

Aspose.Cells.Properties.DocumentProperty类允许检索文档属性的名称、值和类型:

成员名称 描述 ToXXX方法
Boolean 属性数据类型为布尔值 ToBool
Date 属性数据类型为日期时间。请注意,Microsoft Excel仅存储日期部分,无法在此类型的自定义属性中存储时间 ToDateTime
Float 属性数据类型为双精度浮点数 ToDouble
Number 属性数据类型为Int32 ToInt
String 属性数据类型为字符串 ToString
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Accessing a custom document property
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties[0];
// Storing the value of the document property as an object
object objectValue = customProperty1.Value;
// Accessing a custom document property
Aspose.Cells.Properties.DocumentProperty customProperty2 = customProperties[1];
// Checking the type of the document property and then storing the value of the
// document property according to that type
if (customProperty2.Type == Aspose.Cells.Properties.PropertyType.String)
{
string value = customProperty2.Value.ToString();
Console.WriteLine(customProperty2.Name + " : " + value);
}

如何添加或删除自定义文档属性

正如我们在本主题开头所述的那样,开发人员无法添加或删除内置属性,因为这些属性是系统定义的,但可以添加或删除自定义属性,因为这些是用户定义的。

如何添加自定义属性

Aspose.Cells API已经为CustomDocumentPropertyCollection类公开了Add方法,以便将自定义属性添加到集合中。Add方法将属性添加到Excel文件,并返回新文档属性的引用,作为Aspose.Cells.Properties.DocumentProperty对象。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Adding a custom document property to the Excel file
Aspose.Cells.Properties.DocumentProperty publisher = customProperties.Add("Publisher", "Aspose");
// Saving resultant spreadsheet
workbook.Save(dataDir + "out_sample-document-properties.xlsx");

如何配置’链接到内容’自定义属性

要创建与给定范围的内容链接的自定义属性,请调用CustomDocumentPropertyCollection.AddLinkToContent方法并传递属性名称和来源。您可以使用DocumentProperty.IsLinkedToContent属性检查属性是否配置为链接到内容。此外,还可以使用DocumentProperty类的Source属性获取源范围。

我们在示例中使用了一个简单的模板Microsoft Excel文件。工作簿有一个命名范围标记为MyRange,它指向单元格值。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate an object of Workbook
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Add link to content.
customProperties.AddLinkToContent("Owner", "MyRange");
// Accessing the custom document property by using the property name
Aspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["Owner"];
// Check whether the property is lined to content
bool islinkedtocontent = customProperty1.IsLinkedToContent;
// Get the source for the property
string source = customProperty1.Source;
// Save the file
workbook.Save(dataDir + "out_sample-document-properties.xlsx");

如何移除自定义属性

要使用Aspose.Cells删除自定义属性,调用DocumentPropertyCollection.Remove方法并传递要移除的文档属性的名称。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a Workbook object
// Open an Excel file
Workbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");
// Retrieve a list of all custom document properties of the Excel file
Aspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;
// Removing a custom document property
customProperties.Remove("Publisher");
// Save the file
workbook.Save(dataDir + "out_sample-document-properties.xlsx");

高级主题