2015年12月1日 | Leave a comment 问题: 代码 Workbook wb = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(file))); Sheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); for (int i = 0; i <= lastRowNum; ++i) { Row row = sheet.getRow(i); } 123456 Workbook wb = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(file))); Sheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); for (int i = 0; i <= lastRowNum; ++i) { Row row = sheet.getRow(i); } 报错: org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204) at me.yek.weileicrm.activity.impl.FileGroup.list(FileGroup.java:55) at me.yek.weileicrm.activity.impl.FileGroupTest.testFile(FileGroupTest.java:31) at me.yek.weileicrm.activity.impl.FileGroupTest.testList(FileGroupTest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:178) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:662) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:269) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39) ... 33 more 1234567891011121314151617181920212223242526272829303132333435363738394041 org.apache.poi.POIXMLException: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.util.PackageHelper.open(PackageHelper.java:41) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204) at me.yek.weileicrm.activity.impl.FileGroup.list(FileGroup.java:55) at me.yek.weileicrm.activity.impl.FileGroupTest.testFile(FileGroupTest.java:31) at me.yek.weileicrm.activity.impl.FileGroupTest.testList(FileGroupTest.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: Package should contain a content type part [M1.13] at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:178) at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:662) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:269) at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39) ... 33 more 解决: HSSFWorkbook 不支持 xslx,需要XSSFWorkbook 操作xslx,使用 HSSFWorkbook 操作xls 报错时试试使用 XSSFWorkbook, Workbook wb=null; try { wb = new XSSFWorkbook(new FileInputStream(file)); } catch (org.apache.poi.POIXMLException ex) { ex.printStackTrace(); if (ex.getCause() instanceof org.apache.poi.openxml4j.exceptions.InvalidFormatException) { wb = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(file))); }else{ throw ex; } } Sheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); for (int i = 0; i <= lastRowNum; ++i) { Row row = sheet.getRow(i); } 12345678910111213141516 Workbook wb=null; try { wb = new XSSFWorkbook(new FileInputStream(file)); } catch (org.apache.poi.POIXMLException ex) { ex.printStackTrace(); if (ex.getCause() instanceof org.apache.poi.openxml4j.exceptions.InvalidFormatException) { wb = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(file))); }else{ throw ex; } } Sheet sheet = wb.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); for (int i = 0; i <= lastRowNum; ++i) { Row row = sheet.getRow(i); }