Apache POI,同时使用 XSSF 和 HSSF

发布于 2024-10-04 08:26:17 字数 282 浏览 10 评论 0原文

我对 Apache POI 项目有疑问。

我无法在“相同的 Java 类” 中使用 XSSFHSSF。我应该下载哪个 jar 或应该将哪个工件添加到 Maven 中?

我想同时处理 xlsxlsx 文件。当我收到 Excel 版本错误时,我会将XSSF 更改为 HSSFHSSF 更改为 XSSF

我该怎么做?

I have a problem with Apache POI project.

I failed to use XSSF and HSSF in the "Same Java Class". Which jar should I download or which artifact should I get add into maven?

I want to handle both xls and xlsx files at the same time. When I get excel version error, I will change the XSSF to HSSF or HSSF to XSSF.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

也只是曾经 2024-10-11 08:26:17

与其这样做,不如尝试使用新版本的 Apache POI 3.7,它具有 SS 包,可以处理 HSSF 和 XSSF,而不必担心

此处的类型详细信息:http://poi.apache.org/spreadsheet/index.html

Instead of doing that, try using the new release of Apache POI 3.7, it has SS package which handles both HSSF and XSSF without worrying about type

Details here: http://poi.apache.org/spreadsheet/index.html

月下伊人醉 2024-10-11 08:26:17

除了“标准”SS 包解决方案之外,您还可以简单地使用 if 语句 将正确的工作簿格式正确加载到工作簿界面对象,就像这样:

Workbook workbook; //<-Interface, accepts both HSSF and XSSF.
File file = new File("YourExcelFile.xlsx");
if (FileUtils.getFileExt(file).equalsIgnoreCase("xls")) {
  workbook = new HSSFWorkbook(new FileInputStream(file));
} else if (FileUtils.getFileExt(file).equalsIgnoreCase("xlsx")) {
  workbook = new XSSFWorkbook(new FileInputStream(file));
} else {
  throw new IllegalArgumentException("Received file does not have a standard excel extension.");
}

Aside from the "standard" SS package solution, you can also simply use an if statement to correctly load the right workbook format into an Workbook interface object, like so:

Workbook workbook; //<-Interface, accepts both HSSF and XSSF.
File file = new File("YourExcelFile.xlsx");
if (FileUtils.getFileExt(file).equalsIgnoreCase("xls")) {
  workbook = new HSSFWorkbook(new FileInputStream(file));
} else if (FileUtils.getFileExt(file).equalsIgnoreCase("xlsx")) {
  workbook = new XSSFWorkbook(new FileInputStream(file));
} else {
  throw new IllegalArgumentException("Received file does not have a standard excel extension.");
}
何以畏孤独 2024-10-11 08:26:17

使用同时处理 xssf 和 hssf 的工厂

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

Workbook wb = WorkbookFactory.create(new File("file"))

Use the factory instead which handles both xssf and hssf

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

Workbook wb = WorkbookFactory.create(new File("file"))
羁拥 2024-10-11 08:26:17

这对我有用:

    filePath = "C:\Users\user1\workspace\myproject\Excel.xlsx"
    String extension = FilenameUtils.getExtension(filePath);
    System.out.println(extension);

This worked for me:

    filePath = "C:\Users\user1\workspace\myproject\Excel.xlsx"
    String extension = FilenameUtils.getExtension(filePath);
    System.out.println(extension);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文