本文介绍如何使用 C# 在 Excel 中显示文档属性。它包含设置 IDE 以使用此功能的详细信息、完成任务的步骤列表以及使用 C#** 显示 **Excel 文件属性的示例代码。您将学习使用不同类型的参数访问内置和自定义属性。
使用 C# 在 Excel 中显示文档属性的步骤
- 将 IDE 设置为使用 Aspose.Cells for .NET 读取属性
- 将Excel文件加载到Workbook类中以显示属性
- 循环遍历工作簿中的所有自定义文档属性
- 显示属性名称和值
- 迭代所有 builtin properties 和显示名称和值
- 使用属性名称或索引访问各种属性
这些步骤总结了如何使用 C# 在 Excel 中显示文档属性。加载源 Excel 文件,解析所有自定义/内置属性,并显示一些功能,例如名称和值。根据要求使用属性名称或索引访问各个属性。
使用 C# 显示 Excel 文档属性的代码
using Aspose.Cells; | |
using Aspose.Cells.Properties; | |
using System; | |
class Program | |
{ | |
static void Main(string[] args) // Show Excel Document Properties in C# | |
{ | |
new License().SetLicense("License.lic"); | |
// Open an Excel file | |
Workbook workbook = new Workbook("Input.xlsx"); | |
foreach(DocumentProperty custProp in workbook.CustomDocumentProperties) | |
{ | |
Console.WriteLine($"Workbook Custom Property Name: {custProp.Name}, Value = {custProp.Value}"); | |
} | |
foreach (DocumentProperty builtInProp in workbook.BuiltInDocumentProperties) | |
{ | |
Console.WriteLine($"Workbook Builtin Property Name: {builtInProp.Name}, Value = {builtInProp.Value}"); | |
} | |
// Retrieve a list of all builtin document properties of the Excel file | |
DocumentPropertyCollection builtinProperties = workbook.BuiltInDocumentProperties; | |
DocumentProperty builtinProperty; | |
// Accessing a builtin document property by using the property name | |
builtinProperty = builtinProperties["Author"]; | |
Console.WriteLine(builtinProperty.Name + " " + builtinProperty.Value); | |
// Accessing the same builtin document property by using the property index | |
builtinProperty = builtinProperties[0]; | |
Console.WriteLine(builtinProperty.Name + " " + builtinProperty.Value); | |
Console.WriteLine("Excel document properties displayed successfully"); | |
} | |
} |
此示例代码演示了如何使用 C# 在 Excel 中显示属性。请注意,如果 Excel 文件中可用,则将显示自定义属性,但始终显示内置属性。要访问单个属性,您可以使用任何其他内置属性,例如 Author、LastSavedBy、CreateTime、LastSavedTime 和 Version 等。
本文教我们访问和显示 Excel 文件中的属性。要取消隐藏 Excel 工作表,请参阅有关 如何使用 C# 取消隐藏 Excel 工作表 的文章。