且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 POI SS 打开 .xlsx 文件?

更新时间:2022-05-21 23:49:16

第一:修复异常

有两种解决方案:

First: Fix the Exception

There are two solutions:

  1. 正如 Gagravarr 已经提到的:您需要 dom4j 来修复您的异常.
  2. 正如 Jon 已经提到的:你必须更新你的依赖项,所以你不再需要 dom4j.

如果您使用 Maven,您可以添加必要的依赖项:(也许检查更新版本:Maven 存储库:org.apache.poi)

If you're using Maven, you can add the necessary dependencies with: (Maybe check for newer versions at: Maven Repository: org.apache.poi)

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.12</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

然后:打开文件

如果您修复了异常,您可以使用以下代码打开您的 file.xlsx 文件:

String path = "Relative/Path/To/Your/File/file.xlsx";
File file = new File(path);

XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
// Use your sheet ...

更多提示

  • 与 Gagravarr 一样,我也建议使用文件而不是文件输入流.
  • 如果你想打开某个工作表,你可以使用 workbook.getSheet(String name);
  • 如果根据您的项目不知道文件的相对路径,您可以使用 System.out.println("Relative path:" + System.getProperty("user.dir"));
  • 问候,winklerrr

    Regards, winklerrr