我正在编写一些代码来将 Excel 文件导入数据库。这些文件可能很大(数千行),所以我使用的是事件 API。POI 版本为 3.9
我正在打开这样的文件: FileInputStream fin = new FileInputStream(file);
//create record listener
HSSFRecordListener mainListener = new HSSFRecordListener("aaa.xls");
// create a new org.apache.poi.poifs.filesystem.Filesystem
POIFSFileSystem poifs = new POIFSFileSystem(fin);
// get the Workbook (excel part) stream in a InputStream
din = poifs.createDocumentInputStream("Workbook");
有些文件在最后一行抛出 FileNotFoundException。事实上,如果我用 7zip 打开这些文件,就没有Workbook
条目,而是有Book
。
如果找不到,我试图通过打开Book
条目来解决这个问题。Workbook
try {
din = poifs.createDocumentInputStream("Workbook");
} catch (FileNotFoundException e) {
try {
din = poifs.createDocumentInputStream("Book");
} catch (FileNotFoundException e1) {
FileNotFoundException e2 = new FileNotFoundException("Neither Workbook nor Book found in file!");
e2.initCause(e1);
throw e2;
}
}
这导致另一个异常:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.apache.poi.hssf.record.RecordFormatException: Unable to construct record instance
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
org.apache.poi.hssf.record.RecordFormatException: Unable to construct record instance
org.apache.poi.hssf.record.RecordFactory$ReflectionConstructorRecordCreator.create(RecordFactory.java:65)
org.apache.poi.hssf.record.RecordFactory.createSingleRecord(RecordFactory.java:301)
org.apache.poi.hssf.record.RecordFactoryInputStream$StreamEncryptionInfo.<init>(RecordFactoryInputStream.java:65)
org.apache.poi.hssf.record.RecordFactoryInputStream.<init>(RecordFactoryInputStream.java:182)
org.apache.poi.hssf.eventusermodel.HSSFEventFactory.genericProcessEvents(HSSFEventFactory.java:139)
org.apache.poi.hssf.eventusermodel.HSSFEventFactory.processEvents(HSSFEventFactory.java:106)
pl.veracomp.service.SpreadsheetImportService.process(SpreadsheetImportService.java:369)
pl.veracomp.controller.uploadController.onSubmit(uploadController.java:57)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause
org.apache.poi.hssf.record.RecordFormatException: Not enough data (0) to read requested (2) bytes
org.apache.poi.hssf.record.RecordInputStream.checkRecordPosition(RecordInputStream.java:216)
org.apache.poi.hssf.record.RecordInputStream.readShort(RecordInputStream.java:233)
org.apache.poi.hssf.record.InterfaceHdrRecord.<init>(InterfaceHdrRecord.java:43)
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
org.apache.poi.hssf.record.RecordFactory$ReflectionConstructorRecordCreator.create(RecordFactory.java:57)
org.apache.poi.hssf.record.RecordFactory.createSingleRecord(RecordFactory.java:301)
org.apache.poi.hssf.record.RecordFactoryInputStream$StreamEncryptionInfo.<init>(RecordFactoryInputStream.java:65)
org.apache.poi.hssf.record.RecordFactoryInputStream.<init>(RecordFactoryInputStream.java:182)
org.apache.poi.hssf.eventusermodel.HSSFEventFactory.genericProcessEvents(HSSFEventFactory.java:139)
org.apache.poi.hssf.eventusermodel.HSSFEventFactory.processEvents(HSSFEventFactory.java:106)
pl.veracomp.service.SpreadsheetImportService.process(SpreadsheetImportService.java:369)
pl.veracomp.controller.uploadController.onSubmit(uploadController.java:57)
org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:272)
org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:268)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
谷歌有一些关于POI 3.2 和 3.7 中的已修复错误的信息,这些错误与异常有关Not enough data (0) to read requested (2) bytes
,但似乎是另一回事。
相同的文件可以在 Excel 2007 中成功打开。当我使用 手动保存它们时Save As=>Excel 97/2003
,7zip 显示Book
条目已替换为Workbook
,我可以使用 Apache POI 成功导入它们。
有没有人发现这个问题?如何解决它?
编辑
问题是当我尝试打开以 Microsoft Excel 5.0/95 文件格式保存的文件时。
要重现此问题,请创建新电子表格,输入任何数据,然后另存为 => Microsoft Excel 5.0/95 工作簿 (*.xls)。
有什么方法可以使用 apache POI 读取这种格式,还是我必须强制我的用户在上传之前升级他们的工作簿?