通过 Apache POI 读取 XLSM 工作簿时出现异常 [英] Exception when reading XLSM workbook via Apache POI

查看:60
本文介绍了通过 Apache POI 读取 XLSM 工作簿时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apache POI 库 (3.8-beta5) 读取 .xlsm 文件:

I'm trying to read .xlsm file using Apache POI library (3.8-beta5):

Workbook wb = null;
try {
    wb =  WorkbookFactory.create(isXLSFile);
} catch (IOException e) {
...

没什么复杂的.虽然一般文档都可以很好地阅读,但有一个文档会引发异常:

nothing complicated. While generally documents are being read well, one document throws an exception:

Caused by: java.lang.IllegalStateException: A sheet hyperlink must either have a location, or a relationship. Found:
<xml-fragment ref="H13" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"/>
    at org.apache.poi.xssf.usermodel.XSSFHyperlink.<init>(XSSFHyperlink.java:72)
    at org.apache.poi.xssf.usermodel.XSSFSheet.initHyperlinks(XSSFSheet.java:250)
    at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:203)
    at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:175)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:260)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:174)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:67)

有趣的是,如果我在 LibreOffice 中打开文件(我的机器上没有 MS Office)并重新保存它(保持格式),那么文档可以正常读取.据我所知,这个问题与文档结构的有效性有关 - 所以这是数据问题而不是库代码中的错误(或者不是吗?).但也许有办法抑制此类错误?

It's interesting that if I open file in LibreOffice (no MS Office on my machine) and re-save it (keeping the format) then document is read fine. As I understand this problem has something to do with validity of document structure - so this is data problem and not error in library code (or isn't it?). But maybe there is a way to suppress such errors?

[更新] 已接受答案的评论中提到的修复已集成到 2012 年 3 月 26 日发布的 Apache POI 3.8 版本中.

[UPDATE] The fix mentioned in comments to accepted answer was integrated in 3.8 version of Apache POI released on March 26th 2012.

推荐答案

我刚刚在 Google Refine 错误报告中从用户那里得到了这样的文件.我的结论是,这是一个 Apache POI 错误,他们抱怨未使用的引用过于严格.此外,抛出一个 RuntimeException 而不是一个声明的(检查的)异常有点粗鲁.我已提交此错误报告:https://issues.apache.org/bugzilla/show_bug.cgi?id=52716

I just got a file like this from a user in a Google Refine bug report. My conclusion is that it's an Apache POI bug and they're being overly strict by complaining about a reference which isn't used. Additionally, throwing a RuntimeException instead of a declared (checked) exception is kind of rude. I've filed this bug report: https://issues.apache.org/bugzilla/show_bug.cgi?id=52716

这篇关于通过 Apache POI 读取 XLSM 工作簿时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆