问题描述
代码如下:InputStreaminp=newFileInputStream(path);//使用workbookFactory实例化workbook对象Workbookwb=WorkbookFactory.create(inp);intcolumnNum=0;//得到要读取的sheetSheetsheet=wb.getSheetAt(sheetIndex);//得到当前sheet中的所有行的行数if(sheet.getRow(0)!=null){columnNum=sheet.getRow(0).getLastCellNum()-sheet.getRow(0).getFirstCellNum();}.......导入小文件EXCEL时可通过。导入大文件(4000+行)时,走到Workbookwb=WorkbookFactory.create(inp);卡住抛错报错误在一楼:
解决方案
解决方案二:
org.apache.poi.POIXMLException:java.lang.reflect.InvocationTargetExceptionatorg.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)atorg.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)atorg.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)atorg.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183)atorg.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73)atcom.rier.core.dao.excelReader.getrowCellnum(excelReader.java:215)atcom.rier.keysafety.auth.action.ExportWordAction.importInfo(ExportWordAction.java:362)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:597)atcom.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)atcom.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)atcom.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)atcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)atorg.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)atcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)atcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)atcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atorg.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atorg.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atorg.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:314)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atorg.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)atcom.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atorg.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atcom.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)atorg.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)atorg.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)atorg.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)atorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atcom.rier.keysafety.filter.LoginFilter.doFilter(LoginFilter.java:86)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)atorg.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)atorg.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:600)atorg.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1703)atjava.lang.Thread.run(Thread.java:619)
解决方案三:
Causedby:java.lang.reflect.InvocationTargetExceptionatsun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeMethod)atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)atjava.lang.reflect.Constructor.newInstance(Constructor.java:513)atorg.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)...74moreCausedby:java.lang.OutOfMemoryError:Javaheapspaceatorg.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1592)atorg.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1223)atorg.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1144)atorg.apache.xmlbeans.impl.store.Saver$TextSaver.emitElement(Saver.java:926)atorg.apache.xmlbeans.impl.store.Saver.processElement(Saver.java:456)atorg.apache.xmlbeans.impl.store.Saver.process(Saver.java:307)atorg.apache.xmlbeans.impl.store.Saver$TextSaver.saveToString(Saver.java:1727)atorg.apache.xmlbeans.impl.store.Cursor._xmlText(Cursor.java:546)atorg.apache.xmlbeans.impl.store.Cursor.xmlText(Cursor.java:2436)atorg.apache.xmlbeans.impl.values.XmlObjectBase.xmlText(XmlObjectBase.java:1455)atorg.apache.poi.xssf.model.SharedStringsTable.getKey(SharedStringsTable.java:130)atorg.apache.poi.xssf.model.SharedStringsTable.readFrom(SharedStringsTable.java:120)atorg.apache.poi.xssf.model.SharedStringsTable.<init>(SharedStringsTable.java:102)atsun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeMethod)atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)atjava.lang.reflect.Constructor.newInstance(Constructor.java:513)atorg.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)atorg.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)atorg.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)atorg.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183)atorg.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73)atcom.rier.core.dao.excelReader.getrowCellnum(excelReader.java:215)atcom.rier.keysafety.auth.action.ExportWordAction.importInfo(ExportWordAction.java:362)atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)atjava.lang.reflect.Method.invoke(Method.java:597)atcom.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)atcom.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)atcom.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)atcom.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
解决方案四:
检查类库对不对
解决方案五:
内存溢出了,你把VM的内存设置大一些!
解决方案六:
引用3楼wyx100的回复:
检查类库对不对
小的文件是没有问题的,4000条以上的文件才会出现这个问题。
解决方案七:
4楼说得对,还有excel最好不要有样式
解决方案八:
这说明你的输入流就出现了问题
解决方案九:
privatestaticList<ContactsManagementInfo>read2003Excel(MultipartFilefile)throwsIOException{InputStreaminput=file.getInputStream();List<ContactsManagementInfo>contactsManagementInfoList=newArrayList<ContactsManagementInfo>();try{HSSFWorkbookhwb=newHSSFWorkbook(input);HSSFSheetsheet=hwb.getSheetAt(0);ContactsManagementInfoci=null;HSSFRowrow=null;HSSFCellcell=null;intcounter=1;for(inti=sheet.getFirstRowNum();counter<sheet.getPhysicalNumberOfRows();i++){ci=newContactsManagementInfo();row=sheet.getRow(i+1);for(intj=row.getFirstCellNum();j<=row.getLastCellNum();j++){cell=row.getCell(j);switch(j){case0:ci.setId(Integer.parseInt(cell.getStringCellValue()));break;case1:ci.setDepartmentId(Integer.parseInt(cell.getStringCellValue()));break;case2:ci.setDepartmentName(cell.getStringCellValue());break;case3:ci.setName(cell.getStringCellValue());break;case4:ci.setRoomNum(cell.getStringCellValue());break;case5:ci.setOfficePhone(cell.getStringCellValue());break;case6:ci.setFaxNum(cell.getStringCellValue());break;case7:ci.setTelephone(cell.getStringCellValue());break;default:break;}}counter++;contactsManagementInfoList.add(ci);}}catch(Exceptione){e.printStackTrace();}returncontactsManagementInfoList;}
你这是输入流就出现问题了,可能是数据格式有问题,上面方法可以参考一下我实现的导入功能
解决方案十:
引用6楼sun403的回复:
4楼说得对,还有excel最好不要有样式
样式我excel导几万行都没事不是样式问题