You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Java(Apache POI)读取嵌入的Excel文件?

Java读取嵌入在Excel中的Excel文件方法

嵌入在Excel里的文件是以OLE对象形式存储的,用Apache POI可以实现读取,步骤如下:

核心思路

  1. 遍历主Excel的工作表,找到所有绘图对象中的OLE嵌入对象
  2. 判断嵌入对象是否为Excel文件(通过文件名后缀或内容类型)
  3. 提取嵌入对象的输入流,用对应的POI Workbook类加载读取

代码实现

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.*;

public class ReadEmbeddedExcel {
    public static void main(String[] args) throws IOException {
        // 主Excel文件路径
        File mainXlsxFile = new File("main.xlsx");
        
        try (FileInputStream fis = new FileInputStream(mainXlsxFile);
             XSSFWorkbook mainWorkbook = new XSSFWorkbook(fis)) {

            // 遍历主工作簿的所有工作表
            for (XSSFSheet sheet : mainWorkbook) {
                // 获取工作表中的绘图容器(嵌入对象存放在这里)
                for (XSSFDrawing drawing : sheet.getDrawings()) {
                    for (XSSFShape shape : drawing.getShapes()) {
                        // 判断当前形状是否是嵌入对象
                        if (shape instanceof XSSFObjectData) {
                            XSSFObjectData objectData = (XSSFObjectData) shape;
                            String embeddedFileName = objectData.getFileName();

                            // 筛选出Excel格式的嵌入文件
                            if (embeddedFileName != null && 
                                (embeddedFileName.endsWith(".xlsx") || embeddedFileName.endsWith(".xls"))) {
                                
                                // 获取嵌入文件的输入流
                                try (InputStream embeddedStream = objectData.getInputStream()) {
                                    // 根据文件类型初始化对应的Workbook
                                    Workbook embeddedWorkbook;
                                    if (embeddedFileName.endsWith(".xlsx")) {
                                        embeddedWorkbook = new XSSFWorkbook(embeddedStream);
                                    } else {
                                        embeddedWorkbook = new HSSFWorkbook(embeddedStream);
                                    }

                                    // 读取嵌入Excel的内容,示例为读取第一个工作表
                                    Sheet embeddedSheet = embeddedWorkbook.getSheetAt(0);
                                    for (Row row : embeddedSheet) {
                                        for (Cell cell : row) {
                                            // 按需处理单元格数据,这里直接打印
                                            System.out.print(cell.toString() + "\t");
                                        }
                                        System.out.println();
                                    }

                                    embeddedWorkbook.close();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

注意事项

  • 确保使用Apache POI 4.1.2及以上版本,旧版本对OLE对象的支持不完善
  • 项目需要引入poi和poi-ooxml依赖,对应处理.xls和.xlsx格式
  • 如果嵌入对象没有文件名,可以通过内容类型判断:.xlsx对应application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,.xls对应application/vnd.ms-excel

内容的提问来源于stack exchange,提问作者Karthick88it

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 08:28:24