当使用POI读取.xlsx文件时,出现错误“Zip File is closed”

public Sheet readExcel() throws Exception{ //File fi=new File(new File(System.getProperty("user.dir"))+"\\src\\testdata2.xls"); File fi=new File("C:\\Users\\admin\\workspace\\HMS\\src\\testdata\\testdata1.xlsx"); Workbook wb = new XSSFWorkbook(fi); Sheet Sheet = wb.getSheetAt(0); int rowCount = Sheet.getLastRowNum()-Sheet.getFirstRowNum(); for (int i = 1; i < rowCount+1; i++) { Row row = Sheet.getRow(i); if(row.getCell(0).toString().length()==0){ System.out.println(row.getCell(1).toString()+"----"+ row.getCell(2).toString()+"----"+ row.getCell(3).toString()+"----"+ row.getCell(4).toString()); } } return Sheet; } 

通过运行上面的代码得到像这样的错误……..

线程“main”中的exceptionjava.lang.IllegalStateException:Zip文件在org.apache.poi.openxml4j.util.ZipFileZipEntrySource.getEntries(ZipFileZipEntrySource.java:45)处closures,位于org.apache.poi.openxml4j.opc.ZipPackage。 getPartsImpl(ZipPackage.java:186)at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:684)at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:254) org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:201)at org.apache.poi.xssf.usermodel.XSSFWorkbook。(XSSFWorkbook.java:294)at ExcelReader.readExcel(ExcelReader.java: 16)在ExcelReader.main(ExcelReader.java:30)

任何人都可以帮我找出究竟是什么问题。

我谷歌search,但无法得到解决scheme!

要读取xslx文件,请使用创buildFileInputStream类的对象

  //Create a object of File class to open xlsx file File file = new File("path/filename.xlsx"); //Create an object of FileInputStream class to read excel file FileInputStream inputStream = new FileInputStream(file); //create object of XSSFWorkbook class Workbook wb = new XSSFWorkbook(inputStream); 

希望这一点你…

尝试缩短文件名和文件path。 看起来像“和”之间的字符有多长时间是有限制的。 它为我工作!

创buildXSSFWorkbook对象时,您不一定需要传递FileInputStream ,也可以将绝对path+文件名作为string传递,并且可能会很长(对于101个字符,108个字符的双斜杠,可能会更长)。 我只是写了一个小的本地应用程序在Windows 7下,其唯一的参数是一个属性文件,其中包含(其他属性)我想要处理的.xlsx文件的绝对path+文件名(例如属性格式: datasetFile=C:\\Users\\jlm\\Documents\\Test Cases\\AAAS\\TestCase2JsonGenerator\\AAAS_g1.xlsx )。 我只是将datasetFile属性作为parameter passing给XSSFWorkbook构造函数(示例代码行: wb = new XSSFWorkbook(tags.get("datasetFile")); )。 它工作得很好,但不要忘记任何双斜杠,否则你会得到“Zip File is closed”exception(大约2个小时)。