本快速指南介绍了如何使用 Java 显示 Excel 中的文档属性。它具有用于设置 IDE 进行开发的资源、此功能所需的步骤列表以及使用 Java 打印 Excel 电子表格属性 的示例代码。您将获得有关各种文档属性的信息,并根据需要以不同的方式访问它们。
使用 Java 显示 Excel 文件属性的步骤
- 使用 Aspose.Cells for Java 设置 IDE 以打印 Excel 文件属性
- 将 Excel 文件加载到 Workbook 并迭代自定义属性集合
- 将对象类型更改为 DocumentProperty 并显示名称和值
- 遍历所有内置属性并显示名称和值
- 使用名称或索引访问各个属性并显示各种参数
这些步骤有助于使用 Java* 访问和显示 *Excel 电子表格属性。加载 Excel 电子表格并访问其中的 CustomDocumentProperties 和builtInDocumentProperties 集合。在每次迭代中,将对象类型转换为 DocumentProperty 并显示名称、值和其他可用信息。
使用 Java 打印 Excel 工作簿属性的代码
import com.aspose.cells.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Display Excel file properties in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Open an Excel file | |
Workbook workbook = new Workbook("Input.xlsx"); | |
for(Object obj : workbook.getCustomDocumentProperties()) | |
{ | |
DocumentProperty custProp = (DocumentProperty)obj; | |
System.out.println("Workbook Custom Property Name: " + custProp.getName() + ", Value = " + custProp.getValue()); | |
} | |
for (Object obj : workbook.getBuiltInDocumentProperties()) | |
{ | |
DocumentProperty builtInProp = (DocumentProperty)obj; | |
System.out.println("Workbook Builtin Property Name: " + builtInProp.getName() + ", Value = " + builtInProp.getValue()); | |
} | |
// Retrieve a list of all builtin document properties of the Excel file | |
DocumentPropertyCollection builtinProperties = workbook.getBuiltInDocumentProperties(); | |
DocumentProperty builtinProperty; | |
// Accessing a builtin document property by using the property name | |
builtinProperty = builtinProperties.get("Author"); | |
System.out.println(builtinProperty.getName() + " " + builtinProperty.getValue()); | |
// Accessing the same builtin document property by using the property index | |
builtinProperty = builtinProperties.get(0); | |
System.out.println(builtinProperty.getName() + " " + builtinProperty.getValue()); | |
System.out.println("Done"); | |
} | |
} |
此代码演示如何使用 Java 在 Excel 中显示文档属性。 DocumentProperty 包含显示的名称和值,但是,您可以在其中使用其他方法和属性,例如源、哈希代码、isLinkedToContent、toDateTime 等。Excel 文件具有可以访问和显示的各种属性,例如标题、主题、作者、评论、LastSavedBy 和 CreateTime。
本文教我们如何访问 Excel 文件属性并显示它们。如果您想在 Excel 文件中应用过滤器,请参阅有关 如何使用 Java 在 Excel 中应用过滤器 的文章。