用 Java 读取 XLSX 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/267025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:14:39  来源:igfitidea点击:

Read XLSX file in Java

javaexcelparsing

提问by MrPortico

I need to read an Excel 2007 XLSX file in a Java application. Does anyone know of a good API to accomplish this task?

我需要在 Java 应用程序中读取 Excel 2007 XLSX 文件。有谁知道一个很好的 API 来完成这个任务?

采纳答案by Tobias Schulte

AFAIK there are no xlsx-libraries available yet. But there are some for old xls:

AFAIK 还没有可用的 xlsx 库。但是对于旧的 xls 有一些:

One library is jxlswhich internally uses the already mentioned POI.

一个库是jxls,它在内部使用已经提到的POI

2 other links: Handle Excel files, Java libraries to read and write Excel XLS document files.

2个其他链接:处理Excel文件Java库读写Excel XLS文档文件

回答by hova

I had to do this in .NET and I couldn't find any API's out there. My solution was to unzip the .xlsx, and dive right into manipulating the XML. It's not so bad once you create your helper classes and such.

我必须在 .NET 中执行此操作,但找不到任何 API。我的解决方案是解压缩 .xlsx,然后直接操作 XML。一旦你创建了你的助手类等等,情况并没有那么糟糕。

There are some "gotchas" like the nodes all have to be sorted according to the way excel expects them, that I didn't find in the official docs. Excel has its own date timestamping, so you'll need to make a conversion formula.

有一些“陷阱”,比如节点都必须根据 excel 期望的方式进行排序,我在官方文档中没有找到。Excel 有自己的日期时间戳,因此您需要创建一个转换公式。

回答by Josh

Have you looked at the poorly obfuscated API?

你有没有看过模糊不清的 API

Nevermind:

没关系:

HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. It does not support the new Excel 2007 .xlsx OOXML file format, which is not OLE2 based.

HSSF 是 POI 项目的 Excel '97(-2007) 文件格式的纯 Java 实现。它不支持新的 Excel 2007 .xlsx OOXML 文件格式,该格式不是基于 OLE2 的。

You might consider using a JDBC-ODBC bridgeinstead.

您可能会考虑改用JDBC-ODBC 桥接器

回答by NR.

I don't know if it is up to date for Excel 2007, but for earlier versions I use the JExcelAPI

我不知道 Excel 2007 是否是最新的,但对于早期版本,我使用JExcelAPI

回答by Tobias Schulte

This one maybe work for you, it can read/write Excel 2007 xlsx file. SmartXLS

这个可能适合你,它可以读/写 Excel 2007 xlsx 文件。 智能XLS

回答by MrPortico

I'm not very happy with any of the options so I ended up requesting the file in Excel 97 formate. The POI works great for that. Thanks everyone for the help.

我对任何选项都不太满意,所以我最终请求 Excel 97 格式的文件。POI 对此非常有用。感谢大家的帮助。

回答by Galbrezu

Might be a little late, but the beta POI now supports xlsx.

可能有点晚了,但 beta POI 现在支持 xlsx。

回答by David Rabinowitz

Apache POI3.5 have added support to all the OOXML (docx, xlsx, etc.)

Apache POI3.5 添加了对所有 OOXML(docx、xlsx 等)的支持

See the XSSF sub project

查看XSSF 子项目

回答by JasonPlutext

docx4j now covers xlsx as well.

docx4j 现在也涵盖了 xlsx。

"Why would you use docx4j to do this", I hear you ask, "rather than POI, which focuses on xlsx and binary xls?"

“你为什么要使用 docx4j 来做这个”,我听到你问,“而不是 POI,它专注于 xlsx 和二进制 xls?”

Probably because you like JAXB (as opposed to XML Beans), or you are already using docx4j for docx or pptx, and need to be able to do some stuff with xlsx as well.

可能是因为您喜欢 JAXB(而不是 XML Bean),或者您已经将 docx4j 用于 docx 或 pptx,并且还需要能够使用 xlsx 做一些事情。

Another possible reason is that the jar XML Beans generates from the OpenXML schemas is too big for your purposes. (To get around this, POI offers a 'lite' subset: the 'big' ooxml-schemas-1.0.jar is 14.5 MB! But if you need to support arbitrary spreadsheets, you'll probably need the complete jar). In contrast, the whole of docx4j/pptx4j/xlsx4j weighs in at about the same as POI's lite subset.

另一个可能的原因是从 OpenXML 模式生成的 jar XML Bean 对您的目的来说太大了。(为了解决这个问题,POI 提供了一个 'lite' 子集:'big' ooxml-schemas-1.0.jar 是 14.5 MB!但是如果您需要支持任意电子表格,您可能需要完整的 jar)。相比之下,整个 docx4j/pptx4j/xlsx4j 的权重与 POI 的 lite 子集大致相同。

If you are processing spreadsheets only (ie not docx or pptx), and preceding paragraph is not a concern for you, then you would probably be best off using POI.

如果您只处理电子表格(即不是 docx 或 pptx),并且您不关心前一段,那么您可能最好使用 POI。

回答by Shahzad Latif

Aspose.Cells for Javasupports XLSX format. You may find more details and further help in Aspose.Cells for Java Documentation. Please see if this helps.

Aspose.Cells for Java支持 XLSX 格式。您可以在Aspose.Cells for Java 文档中找到更多详细信息和进一步帮助。请看看这是否有帮助。

Disclosure: I work as developer evangelist at Aspose.

披露:我在 Aspose 担任开发人员布道者。