仅使用 apache poi xssf eventmodel 读取 excel (.xlsx) 文件时出错

发布于 2024-12-03 10:24:02 字数 1425 浏览 1 评论 0原文

我正在尝试使用 apache 站点 http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api

我收到以下错误:

Processing new sheet:

A1 - Have a nice day
Exception in thread "main" java.lang.NumberFormatException: For input string: "Have a nice day"
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1111)
    at ExcelExtract.processAllSheets(ExcelExtract.java:48)
    at ExcelExtract.main(ExcelExtract.java:119)
Caused by: java.lang.NumberFormatException: For input string: "Have a nice day"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at ExcelExtract$SheetHandler.endElement(ExcelExtract.java:99)
    at org.apache.xerces.parsers.SAXParser.endElement(SAXParser.java:1403)
    at org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValidator.java:1550)
    at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:1204)
    at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098)
    ... 2 more

还有没有办法使用 poi 读取 xlsx 文件仅使用 xssf 事件模型而不使用 xerces.jar?如果有其他可用的示例代码,请告知。

I am trying to read an excel file with words and not numeric data using the code from apache site http://poi.apache.org/spreadsheet/how-to.html#xssf_sax_api

I get the following error:

Processing new sheet:

A1 - Have a nice day
Exception in thread "main" java.lang.NumberFormatException: For input string: "Have a nice day"
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1111)
    at ExcelExtract.processAllSheets(ExcelExtract.java:48)
    at ExcelExtract.main(ExcelExtract.java:119)
Caused by: java.lang.NumberFormatException: For input string: "Have a nice day"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at ExcelExtract$SheetHandler.endElement(ExcelExtract.java:99)
    at org.apache.xerces.parsers.SAXParser.endElement(SAXParser.java:1403)
    at org.apache.xerces.validators.common.XMLValidator.callEndElement(XMLValidator.java:1550)
    at org.apache.xerces.framework.XMLDocumentScanner$ContentDispatcher.dispatch(XMLDocumentScanner.java:1204)
    at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.java:381)
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:1098)
    ... 2 more

Also is there any way to read xlsx file using poi xssf event model only without using xerces.jar? Please inform if any other sample code is available.

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

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

发布评论

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

评论(1

深巷少女 2024-12-10 10:24:02

该异常似乎来自您自己的代码 - ExcelExtract 看起来是您的程序,而不是 POI 的核心位。

看起来您正在将包含字符串的单元格视为包含数字。这是行不通的 - 您需要检查单元格的类型,并适当地处理内容。你不能在没有首先确保它是一个 int 的情况下就将它解析为 int !

不过看起来不是 POI 问题

That exception seems to be coming from your own code - ExcelExtract looks to be your program rather than a core bit of POI

It looks like you're treating a cell that contains a string as if it contains a number. That won't work - you need to check the type of the cell, and handle the contents appropriately. You can't just parse something to an int without first ensuring it is one!

Doesn't look to be a POI issue though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文