Excel管理控件Aspose.Cells開發者指南(二十七):管理文件屬性
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 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 類允許檢索名稱,值,然后鍵入文檔屬性:
- 要獲取屬性名稱,請使用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 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 方法,以便將自定義屬性添加到集合中。該添加 方法添加屬性的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 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");
刪除自定義屬性
要使用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 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");
配置“鏈接到內容”自定義屬性
若要創建鏈接到給定范圍的內容的自定義屬性,請調用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 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");
還想要更多嗎?您可以點擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。

QQ交談
在線咨詢

渝公網安備
50010702500608號

客服熱線