Excel管理控件Aspose.Cells开发者指南(二十七):管理文件属性

Aspose.Cells for .NET是Excel电子表格编程API,可加快电子表格管理和处理任务,支持构建具有生成,修改,转换,呈现和打印电子表格功能的跨平台应用程序。 本文重点介绍如何管理文件属性。包括访问文档属性、添加删除自定义属性、配置“链接到内容”自定义属性。

Aspose.Cells for .NET是Excel电子表格编程API,可加快电子表格管理和处理任务,支持构建具有生成,修改,转换,呈现和打印电子表格功能的跨平台应用程序。

在接下来的系列教程中,将为开发者带来Aspose.Cells for .NET的一系列使用教程,例如关于加载保存转换、字体、渲染、绘图、智能标记等等。本文重点介绍如何管理文件属性。包括访问文档属性、添加删除自定义属性、配置“链接到内容”自定义属性。

>>Aspose.Cells for .NET已经更新至v20.3,新增4大新功能,包括支持FLOOR.MATH、支持过滤功能,支持获取工作表的唯一ID以及支持将chart.series.dataLables.TextDirection设置为vertical,提高整行保存条件格式和验证的性能体验

第六章:文件属性

▲第一节:管理文件属性

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

访问文档属性

Aspose.Cells API支持内置和自定义两种文档属性。Aspose.Cells的Workbook 类代表一个Excel文件,并且与Excel文件一样,Workbook 类可以包含多个工作表,每个工作表均由Worksheet 类表示,而工作表的集合由WorksheetCollection 类表示。如下所述,使用WorksheetCollection 访问文件的文档属性。

  • 要访问内置文档属性,请使用WorksheetCollection.BuiltInDocumentProperties。
  • 要访问自定义文档属性,请使用WorksheetCollection.CustomDocumentProperties。

无论是WorksheetCollection.BuiltInDocumentProperties 和WorksheetCollection.CustomDocumentProperties 返回的实例Aspose.Cells.Properties.DocumentPropertyCollection。此集合包含 Aspose.Cells.Properties.DocumentProperty 对象,每个对象代表一个内置或自定义文档属性。

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

// The path to the documents directory.string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);// Instantiate a Workbook object// Open an Excel fileWorkbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");// Retrieve a list of all custom document properties of the Excel fileAspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;// Accessing a custom document property by using the property nameAspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["ContentTypeId"];Console.WriteLine(customProperty1.Name + " " + customProperty1.Value);// Accessing the same custom document property by using the property indexAspose.Cells.Properties.DocumentProperty customProperty2 = customProperties[0];Console.WriteLine(customProperty2.Name + " " + customProperty2.Value);

该Aspose.Cells.Properties.DocumentProperty 类允许检索名称,值,然后键入文档属性:

  • 要获取属性名称,请使用DocumentProperty.Name。
  • 要获取属性值,请使用DocumentProperty.Value。DocumentProperty.Value 将值作为对象返回。
  • 要获取属性类型,请使用DocumentProperty.Type。这将返回PropertyType 枚举值之一。获取属性类型后,请使用DocumentProperty.ToXXX 方法之一来获取适当类型的值,而不要使用DocumentProperty.Value。下 表描述了DocumentProperty.ToXXX方法。
名称 描述 方法
布尔型 属性数据类型为布尔值 ToBool
日期 该属性数据类型为DateTime。请注意,Microsoft Excel仅存储日期部分,不能在此类型的自定义属性中存储任何时间 ToDateTime
浮动 属性数据类型为Double ToDouble
属性数据类型为Int32 ToInt
属性数据类型为字符串 ToString
// The path to the documents directory.string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);// Instantiate a Workbook object// Open an Excel fileWorkbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");// Retrieve a list of all custom document properties of the Excel fileAspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;// Accessing a custom document propertyAspose.Cells.Properties.DocumentProperty customProperty1 = customProperties[0];// Storing the value of the document property as an objectobject objectValue = customProperty1.Value;// Accessing a custom document propertyAspose.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 typeif (customProperty2.Type == Aspose.Cells.Properties.PropertyType.String){    string value = customProperty2.Value.ToString();    Console.WriteLine(customProperty2.Name + " : " + value);}

添加自定义属性

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

// The path to the documents directory.string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);// Instantiate a Workbook object// Open an Excel fileWorkbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");// Retrieve a list of all custom document properties of the Excel fileAspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;// Adding a custom document property to the Excel fileAspose.Cells.Properties.DocumentProperty publisher = customProperties.Add("Publisher", "Aspose");// Saving resultant spreadsheetworkbook.Save(dataDir + "out_sample-document-properties.xlsx");

删除自定义属性

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

// The path to the documents directory.string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);// Instantiate a Workbook object// Open an Excel fileWorkbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");// Retrieve a list of all custom document properties of the Excel fileAspose.Cells.Properties.DocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;// Removing a custom document propertycustomProperties.Remove("Publisher");// Save the fileworkbook.Save(dataDir + "out_sample-document-properties.xlsx");

配置“链接到内容”自定义属性

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

在示例中,我们使用一个简单的模板Microsoft Excel文件。该工作簿具有一个定义为MyRange的命名范围,该范围引用一个单元格值。

// The path to the documents directory.string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);// Instantiate an object of Workbook// Open an Excel fileWorkbook workbook = new Workbook(dataDir + "sample-document-properties.xlsx");// Retrieve a list of all custom document properties of the Excel fileAspose.Cells.Properties.CustomDocumentPropertyCollection customProperties = workbook.Worksheets.CustomDocumentProperties;// Add link to content.customProperties.AddLinkToContent("Owner", "MyRange");// Accessing the custom document property by using the property nameAspose.Cells.Properties.DocumentProperty customProperty1 = customProperties["Owner"];// Check whether the property is lined to contentbool islinkedtocontent = customProperty1.IsLinkedToContent;// Get the source for the propertystring source = customProperty1.Source;// Save the fileworkbook.Save(dataDir + "out_sample-document-properties.xlsx");


还想要更多吗可以点击阅读【2019 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年3月10日
下一篇 2020年3月10日

相关推荐

发表回复

登录后才能评论