excel读取常见问题(涉及格式:xls、xlsx)
- 异常
- org.apache.poi.EncryptedDocumentException: Default password is invalid for salt/verifier/verifierHash
- org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
- org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0xBF09C5BACCB3CEBF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
- java.lang.IndexOutOfBoundsException: Position 28672 past the end of the file
- 其它知识
异常
org.apache.poi.EncryptedDocumentException: Default password is invalid for salt/verifier/verifierHash
报错截图:
问题原因:
有密码的文档,密码错误
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
报错截图:
问题原因:
文件后缀为xls后缀,其实是xlsx文件
解决方案:
使用XSSF代替HSSF,使用读取xlsx的方法读取该文件
org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0xBF09C5BACCB3CEBF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
问题原因:
文件后缀为xls后缀,其实是xlsx文件
解决方案:
打开源文件,另存为xlsx文件,再读取
java.lang.IndexOutOfBoundsException: Position 28672 past the end of the file
问题原因:
文件错误
其它知识
HSSF、XSSF、SXSSF 的区别
POI提供了HSSF、XSSF以及SXSSF三种方式操作Excel。他们的区别如下:
HSSF:是操作Excel97-2003版本,扩展名为.xls。
XSSF:是操作Excel2007版本开始,扩展名为.xlsx。
SXSSF:是在XSSF基础上,POI3.8版本开始提供的一种支持低内存占用的操作方式,扩展名为.xlsx。