使用 Java 从 XLSX 读取元数据

在当代软件开发中,从文件中提取和处理元数据的能力在各种应用程序中起着关键作用。元数据包括作者、创建日期和文档属性等数据,可提供对文件的宝贵见解并促进有效的文件管理。特别是对于 .xlsx 格式的 Excel 文件,以编程方式访问元数据被证明具有显著的优势。本指南深入探讨了如何使用 Java 编程语言从 XLSX 读取元数据的过程。在这里,您将找到基本步骤以及说明如何 使用 Java 读取 XLSX 元数据 的代码示例。

使用 Java 从 XLSX 读取元数据的步骤

  1. 设置您的 IDE 以利用 GroupDocs.Metadata for Java 从 XLSX 文件中提取元数据
  2. 创建 Metadata 类的实例,将 XLSX 文件的文件路径作为参数传递给其构造函数
  3. 创建规则或条件来检查所有元数据属性
  4. 为 Metadata.findProperties 方法定义条件
  5. 循环遍历检索到的属性

XLSX 是一种广泛使用的电子表格文件格式,尤其与 Microsoft Excel 相关。除了电子表格的实际数据外,XLSX 文件还包含详细说明文件各个方面的元数据。这些元数据包括文档标题、作者、创建和修改日期等信息。以编程方式访问这些元数据使开发人员能够自动执行任务、进行分析或丰富其应用程序中的用户体验。提供的说明与 Windows、macOS 和 Linux 等流行操作系统兼容,前提是您已安装 Java。无需安装其他软件即可在 Java 中提取 XLSX 的元数据。配置推荐的库并相应地调整文件路径后,将以下代码集成到您的项目中应该会顺利进行,不会出现任何问题。

使用 Java 从 XLSX 读取元数据的代码

import com.groupdocs.metadata.Metadata;
import com.groupdocs.metadata.core.FileFormat;
import com.groupdocs.metadata.core.IReadOnlyList;
import com.groupdocs.metadata.core.MetadataProperty;
import com.groupdocs.metadata.core.MetadataPropertyType;
import com.groupdocs.metadata.licensing.License;
import com.groupdocs.metadata.search.FallsIntoCategorySpecification;
import com.groupdocs.metadata.search.OfTypeSpecification;
import com.groupdocs.metadata.search.Specification;
import com.groupdocs.metadata.tagging.Tags;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadMetadataFromXLSXUsingJava {
public static void main(String[] args) {
// Set License to avoid the limitations of Metadata library
License license = new License();
license.setLicense("GroupDocs.Metadata.lic");
Metadata metadata = new Metadata("input.xlsx");
if (metadata.getFileFormat() != FileFormat.Unknown && !metadata.getDocumentInfo().isEncrypted()) {
System.out.println();
// Fetch all metadata properties that fall into a particular category
IReadOnlyList<MetadataProperty> properties = metadata.findProperties(new FallsIntoCategorySpecification(Tags.getContent()));
System.out.println("The metadata properties describing some characteristics of the file content: title, keywords, language, etc.");
for (MetadataProperty property : properties) {
System.out.println(String.format("Property name: %s, Property value: %s", property.getName(), property.getValue()));
}
// Fetch all properties having a specific type and value
int year = Calendar.getInstance().get(Calendar.YEAR);
properties = metadata.findProperties(new OfTypeSpecification(MetadataPropertyType.DateTime).and(new ReadMetadataFromXLSXUsingJava().new YearMatchSpecification(year)));
System.out.println("All datetime properties with the year value equal to the current year");
for (MetadataProperty property : properties) {
System.out.println(String.format("Property name: %s, Property value: %s", property.getName(), property.getValue()));
}
// Fetch all properties whose names match the specified regex
Pattern pattern = Pattern.compile("^author|company|(.+date.*)$", Pattern.CASE_INSENSITIVE);
properties = metadata.findProperties(new ReadMetadataFromXLSXUsingJava().new RegexSpecification(pattern));
System.out.println(String.format("All properties whose names match the following regex: %s", pattern.pattern()));
for (MetadataProperty property : properties) {
System.out.println(String.format("Property name: %s, Property value: %s", property.getName(), property.getValue()));
}
}
}
// Define your own specifications to filter metadata properties
public class YearMatchSpecification extends Specification {
public YearMatchSpecification(int year) {
setValue(year);
}
public final int getValue() {
return auto_Value;
}
private void setValue(int value) {
auto_Value = value;
}
private int auto_Value;
public boolean isSatisfiedBy(MetadataProperty candidate) {
Date date = candidate.getValue().toClass(Date.class);
if (date != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getValue() == calendar.get(Calendar.YEAR);
}
return false;
}
}
public class RegexSpecification extends Specification {
private Pattern pattern;
public RegexSpecification(Pattern pattern) {
this.pattern = pattern;
}
@Override
public boolean isSatisfiedBy(MetadataProperty metadataProperty) {
Matcher matcher = pattern.matcher(metadataProperty.getName());
return matcher.find();
}
}
}

得益于元数据库,使用 Java 从 XLSX 文件元数据中提取见解非常简单。此库允许开发人员访问存储在 XLSX 文件中的关键信息,并轻松将其合并到他们的应用程序中。无论是自动化文档管理、执行数据分析还是增强用户体验,以编程方式访问 XLSX 元数据都为 Java 开发人员提供了大量机会。恭喜!您已成功掌握在 Java 中获取 XLSX 元数据的技术。

在之前的对话中,我们介绍了使用 Java 从 DOCX 文件中提取元数据的详细教程。为了更深入地了解这个主题,我们建议查阅我们的详尽指南如何 使用 Java 从 DOCX 读取元数据

 简体中文