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

如何用Java在Excel 365中创建工作簿并添加行?解决文件未找到异常

解决Java创建Excel 365工作簿的问题

你遇到的FileNotFoundException核心原因是文件路径无效或目标目录不存在,其次HSSFWorkbook仅支持旧版.xls格式,Excel 365推荐使用.xlsx格式,对应POI的XSSFWorkbook。

解决步骤:

1. 修复文件路径问题

  • 替换代码中的"file path"为真实文件路径:Windows系统用双反斜杠\\(如"C:\\Users\\YourName\\Documents\\output.xlsx"),Linux/macOS用单斜杠/(如"/home/yourname/documents/output.xlsx")。
  • 确保路径中的上级目录已存在,比如要写入C:\\ExcelFiles\\test.xlsx,需先创建ExcelFiles文件夹。可通过代码提前递归创建目录:
    File outputFile = new File("C:\\ExcelFiles\\test.xlsx");
    outputFile.getParentFile().mkdirs();
    

2. 使用XSSF创建Excel 365兼容工作簿

HSSFWorkbook针对Excel 97-2003的.xls格式,Excel 365优先支持.xlsx格式,需改用XSSFWorkbook。

修改后的完整代码

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFCell;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class CreateExcelWorkBook {

    public static void main(String[] args) throws IOException {
        // 用try-with-resources自动关闭资源,避免泄漏
        try (XSSFWorkbook workbook = new XSSFWorkbook();
             FileOutputStream fileOut = new FileOutputStream(getOutputFile())) {

            XSSFSheet sheet = workbook.createSheet("FirstSheet");

            // 创建表头行
            XSSFRow rowhead = sheet.createRow(0);
            rowhead.createCell(0).setCellValue("No.");
            rowhead.createCell(1).setCellValue("Name");
            rowhead.createCell(2).setCellValue("Address");
            rowhead.createCell(3).setCellValue("Email");

            // 创建数据行
            XSSFRow row = sheet.createRow(1);
            row.createCell(0).setCellValue("1");
            row.createCell(1).setCellValue("Sankumarsingh");
            row.createCell(2).setCellValue("India");
            row.createCell(3).setCellValue("sankumarsingh@gmail.com");

            // 自动调整列宽,优化可读性
            for (int i = 0; i < 4; i++) {
                sheet.autoSizeColumn(i);
            }

            workbook.write(fileOut);
            System.out.println("Excel文件已生成!");
        }
    }

    private static File getOutputFile() {
        File outputFile = new File("C:\\ExcelFiles\\test.xlsx");
        // 递归创建上级目录
        outputFile.getParentFile().mkdirs();
        return outputFile;
    }
}

必备Maven依赖

如果用Maven管理项目,需在pom.xml中添加POI依赖:

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.5</version>
    </dependency>
</dependencies>

关键说明

  • XSSFWorkbook生成的.xlsx文件完全兼容Excel 365。
  • try-with-resources语法会自动关闭Workbook和输出流,无需手动调用close()。
  • autoSizeColumn方法可自动适配列内容宽度,提升文件易用性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 02:16:07