【POI】解析xls报错:java.util.zip.ZipException: error in opening zip file
今天使用POI解析XLS,报错如下:
Servlet.service() for servlet [rest] in context with path [/cetBrand] threw exception [Request processing failed; nested exception is org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file: 'd:\cet_Brand\examSorce\Sheet名不对.xls'] with root cause java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:219) at java.util.zip.ZipFile.<init>(ZipFile.java:149) at java.util.zip.ZipFile.<init>(ZipFile.java:163) at org.apache.poi.openxml4j.util.ZipSecureFile.<init>(ZipSecureFile.java:142) at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipFile(ZipHelper.java:158) at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:133) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:257) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:202) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:302) at com.junlong.controller.ExamController.uploadMaterial(ExamController.java:259) 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:498)
报错原因是因为:
后台代码:
XSSFWorkbook workbook = new XSSFWorkbook(thisFile); XSSFSheet sheet = workbook.getSheet("成绩单");
这个逻辑没有错 就是我解析的是.xls文件 而使用的是XSSFWorkbook类,应该使用
HSSFWorkbook workbook = new HSSFWorkbook(is); HSSFSheet sheet = workbook.getSheet("成绩单");