使用 GroupDocs.Watermark Java 从 Excel 中提取附件

在当今数据驱动的工作流中,如何从 Excel 工作簿中提取附件 是一个常见需求。无论是整合项目资源、归档合规文档,还是构建自动化报告管道,能够提取嵌入的文件都能节省时间并消除人工错误。在本教程中,你将了解如何为 Java 设置 GroupDocs.Watermark,逐步演示 java extract excel attachments 的代码,并掌握 batch process excel attachments 的最佳实践。

快速答案

  • 哪个库处理 Excel 附件? GroupDocs.Watermark for Java。
  • 哪个方法加载电子表格? new Watermarker(filePath, new SpreadsheetLoadOptions())
  • 可以用 Java 遍历工作表吗? 可以 – 使用 content.getWorksheets() 并循环每个 SpreadsheetWorksheet
  • 生产环境是否需要许可证? 生产使用需要完整的 GroupDocs.Watermark 许可证。
  • 大文件能否正常工作? 能,只要及时关闭 Watermarker 并使用合适的加载选项。

“how to extract attachments” 在 Excel 中的含义是什么?

提取附件指的是检索嵌入在 Excel 工作簿工作表中的任何对象——文件、图像或链接。这些对象以 SpreadsheetAttachment 对象的形式存储,可通过编程方式访问、检查并保存到磁盘。

为什么使用 GroupDocs.Watermark 来提取 Excel 附件?

GroupDocs.Watermark 提供了高级 API,抽象了底层的 Office Open XML 处理,让你专注于业务逻辑而不是文件格式的细节。它还支持 extract embedded objects excel,提供预览图像提取,并在 Java 8+ 环境中保持一致的表现。

前置条件

  • Java Development Kit (JDK) 8 或更高 – 该库可在任何现代 JDK 上运行。
  • IDE – IntelliJ IDEA、Eclipse 或你喜欢的任何编辑器。
  • Maven – 用于依赖管理(也可以手动下载 JAR)。
  • 基础的 Java 知识以及对 Maven 坐标的熟悉。

设置 GroupDocs.Watermark for Java

Maven 配置

pom.xml 中添加 GroupDocs 仓库和依赖:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/watermark/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-watermark</artifactId>
      <version>24.11</version>
   </dependency>
</dependencies>

直接下载(可选)

如果不想使用 Maven,可从 GroupDocs.Watermark for Java releases 获取最新 JAR。

许可证获取步骤

  • 免费试用: 在 GroupDocs 门户注册,获取限时试用。
  • 临时许可证: 开发期间使用临时密钥。
  • 完整许可证: 购买生产许可证,享受无限使用。

基本初始化与设置

创建指向 Excel 文件的 Watermarker 实例:

import com.groupdocs.watermark.Watermarker;

public class DocumentSetup {
    public static void main(String[] args) {
        String filePath = "YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx";
        Watermarker watermarker = new Watermarker(filePath);
        
        // Your code to manipulate the document goes here
        
        watermarker.close();
    }
}

如何从 Excel 中提取附件 – 步骤指南

加载并准备电子表格

首先使用 SpreadsheetLoadOptions 加载工作簿,以便库识别这是一个 Excel 文件:

import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
import com.groupdocs.watermark.Watermarker;

public class ExtractAttachments {
    public static void extract() {
        SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
        String filePath = "YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx";
        
        Watermarker watermarker = new Watermarker(filePath, loadOptions);

访问电子表格内容

获取高级内容对象,以便访问工作表及其附件:

import com.groupdocs.watermark.contents.SpreadsheetContent;

// Get the content of the spreadsheet.
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);

遍历工作表 (java iterate excel worksheets java)

遍历每个工作表,再遍历该工作表中的每个附件:

import com.groupdocs.watermark.contents.SpreadsheetWorksheet;

for (SpreadsheetWorksheet worksheet : content.getWorksheets()) {
    for (SpreadsheetAttachment attachment : worksheet.getAttachments()) {

提取附件详情

对于每个 SpreadsheetAttachment,你可以读取其元数据、预览图像以及原始文件字节:

import com.groupdocs.watermark.contents.SpreadsheetAttachment;

// Display alternative text and frame details of each attachment.
System.out.println("Alternative text: " + attachment.getAlternativeText());
System.out.println("Attachment frame x-coordinate: " + attachment.getX());
System.out.println("Attachment frame y-coordinate: " + attachment.getY());
System.out.println("Attachment frame width: " + attachment.getWidth());
System.out.println("Attachment frame height: " + attachment.getHeight());

// Check if a preview image is available and display its size.
int imageSize = (attachment.getPreviewImageContent() != null) ? attachment.getPreviewImageContent().length : 0;
System.out.println("Preview image size: " + imageSize);

if (attachment.isLink()) {
    System.out.println("Full path to the attached file: " + attachment.getSourceFullName());
} else {
    System.out.println("File type: " + attachment.getDocumentInfo().getFileType());
    System.out.println("Name of the source file: " + attachment.getSourceFullName());
    System.out.println("File size: " + attachment.getContent().length);
}

关闭资源

完成后务必释放 Watermarker,以释放内存:

// Close the Watermarker to release resources.
watermarker.close();

实际应用场景

  • 自动化数据整合: 从一批电子表格中提取所有附件,供数据湖使用。
  • 文档归档: 将提取的附件与原始工作簿一起存储,以满足合规审计。
  • 动态报告生成: 将提取的图像或 PDF 作为自定义报告引擎的输入。

批量处理 Excel 附件的性能考虑

  • 内存管理: 每处理完一个文件后调用 watermarker.close();考虑使用 try‑with‑resources 语法。
  • 批量循环: 将文件分成可管理的组(例如一次 20‑30 个)处理,避免 JVM 堆内存过载。
  • 加载选项调优: 调整 SpreadsheetLoadOptions(如禁用不必要的功能),加快超大工作簿的加载速度。

常见问题与解决方案

问题原因解决方案
NullPointerExceptionattachment.getPreviewImageContent()附件没有预览图像。添加空值检查(如代码所示)。
处理大量大文件时内存激增未及时关闭 Watermarker。使用 try { … } finally { watermarker.close(); } 块。
附件未列出使用的 GroupDocs 版本较旧,缺少完整的附件支持。升级到最新的 24.11 版(或更高)。

常见问答

问:可以从受密码保护的 Excel 文件中提取附件吗?
答:可以。在创建 Watermarker 实例时使用相应的重载方法提供密码。

问:此方法同时支持 .xls(BIFF)和 .xlsx 文件吗?
答:GroupDocs.Watermark 同时支持传统的 .xls 和现代的 .xlsx 格式。

问:如何将提取的附件保存到磁盘?
答:通过 attachment.getContent() 获取字节数组,然后写入 FileOutputStream

问:是否可以只提取特定类型的附件(例如 PDF)?
答:在处理前通过 attachment.getDocumentInfo().getFileType() 进行过滤。

问:商业使用需要什么许可证?
答:生产部署需要完整的 GroupDocs.Watermark 许可证。


最后更新: 2025-12-26
测试环境: GroupDocs.Watermark 24.11 for Java
作者: GroupDocs