仅使用 apache poi xssf eventmodel 读取 excel (.xlsx) 文件时出错
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该异常似乎来自您自己的代码 - 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