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

使用XSSFWorkbook读取Excel文件时抛出‘Can't open the specified file’异常求助(Java11+Spring Boot+Apache POI3.14)

Troubleshooting the XSSFWorkbook InvalidOperationException

Let’s walk through why this error is popping up and fix it step by step—this is a super common pitfall when working with Apache POI!

1. The #1 Culprit: Unclosed Output Stream

When you write the workbook to a FileOutputStream, you never close the stream. This causes two big problems:

  • The buffer might not flush all data to disk, leaving you with a corrupted, unreadable Excel file.
  • The file could still be locked by the JVM when you try to read it right after creation.

Fix this with try-with-resources (a Java 7+ feature that automatically closes streams and workbooks for you):

// Correct way to create and write the workbook
String filePath = "/tmp/templates/" + randomUUId + "_Data.xlsx";
File excelFile = new File(filePath);

// Make sure the parent directory exists first!
File parentDir = excelFile.getParentFile();
if (!parentDir.exists()) {
    parentDir.mkdirs(); // Creates all missing nested directories
}

try (XSSFWorkbook workBook = new XSSFWorkbook();
     FileOutputStream fos = new FileOutputStream(excelFile)) {
    Sheet page = workBook.createSheet("Data");
    // Add any rows/cells you need here
    workBook.write(fos);
} catch (IOException e) {
    // Log the error properly instead of just printing
    System.err.println("Failed to create Excel file: " + e.getMessage());
}

2. Verify File Existence & Permissions

Before attempting to read the file, add quick checks to confirm it’s actually there and accessible:

String readPath = "/tmp/templates/" + randomUUID + "_Data.xlsx";
File readFile = new File(readPath);

if (!readFile.exists()) {
    throw new RuntimeException("Excel file not found at: " + readPath);
}
if (!readFile.canRead()) {
    throw new RuntimeException("No read permissions for file: " + readPath);
}

// Now read the workbook safely
try (XSSFWorkbook actualWorkBook = new XSSFWorkbook(readFile)) {
    // Process your workbook here
} catch (IOException e) {
    System.err.println("Failed to read Excel file: " + e.getMessage());
}

3. Compatibility Issue: Apache POI 3.14 + Java 11

Apache POI 3.14 is extremely outdated (released in 2015) and wasn’t built to support Java 11. This can lead to subtle bugs with file handling via the underlying openxml4j library.

Upgrade to a newer, stable version of POI that supports Java 11:

  • For Spring Boot, update your pom.xml (or build.gradle) to use a recent version:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version> <!-- 4.x supports Java 8+; use 5.2.5+ for full Java 11 support -->
</dependency>

Final Quick Checks

  • Double-check that randomUUId and randomUUID are using the exact same value (typos happen easily!): the ID used to create the file must match the one used to read it.
  • If running in a container (like Docker), confirm that /tmp/templates is properly mounted or persisted—ephemeral storage can sometimes make files disappear unexpectedly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 11:33:10