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

Java读取Excel文件时抛出java.util.zip.ZipException异常的问题排查求助

解决Java读取Excel时的java.util.zip.ZipException问题

Hey there, let's break down what's causing this issue and how to fix it step by step:

核心原因分析

1. POI组件版本不兼容(最主要诱因)

Looking at your pom.xml, you're using poi 4.0.1 but poi-ooxml 3.9 — these two components are tightly coupled, and mismatched versions will break the internal zip handling logic (since .xlsx files are actually zip archives under the hood). This version mismatch is almost certainly triggering the ZipException you're seeing.

2. 可能的Excel文件损坏

If the version fix doesn't resolve the issue, your testData.xlsx might be corrupted (e.g., incomplete download, failed save operation). A damaged zip structure will throw this error when POI tries to parse it.

3. 代码中的空指针隐患

When the XSSFWorkbook initialization fails, workbook remains null, and calling workbook.getSheetAt(0) later throws the NullPointerException you see in the stack trace.


解决步骤

1. 统一POI依赖版本

Update your pom.xml to use the same version for both poi and poi-ooxml. Here's the corrected dependency section:

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3</version>
    </dependency>
    <!-- 统一POI版本,消除兼容性问题 -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.0.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>

After updating, refresh your Maven dependencies to ensure the correct jars are pulled into your project.

2. 验证Excel文件的有效性

  • Try opening testData.xlsx with Microsoft Excel or LibreOffice — if it prompts for repair, the file is corrupted.
  • Replace it with a fresh, properly saved .xlsx file and test again.

3. 修复代码中的异常处理与资源管理

Use try-with-resources to auto-close streams (avoids resource leaks) and add checks for null values to prevent NPEs. Here's the improved code:

public static void main(String[] args) {
    String path = "C:\\testData.xlsx";
    File excelFile = new File(path);

    // 使用try-with-resources自动关闭流和Workbook,避免资源泄漏
    try (FileInputStream fis = new FileInputStream(excelFile);
         XSSFWorkbook workbook = new XSSFWorkbook(fis)) {

        // 检查第一个工作表是否存在
        XSSFSheet sheet = workbook.getSheetAt(0);
        if (sheet == null) {
            System.out.println("第一个工作表不存在!");
            return;
        }

        // 检查第0行是否存在,避免空指针
        XSSFRow row = sheet.getRow(0);
        if (row == null) {
            System.out.println("工作表中第0行不存在!");
            return;
        }

        int totalColumns = row.getPhysicalNumberOfCells();
        System.out.println("Total number of Columns :::" + totalColumns);

    } catch (FileNotFoundException e) {
        System.err.println("未找到指定路径的Excel文件: " + path);
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("读取Excel文件时发生错误: ");
        e.printStackTrace();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 22:03:11