java 使用 POI 读取 .xlsx 文件时出现错误“Zip File is closed”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30423544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When reading .xlsx file using POI I got an error "Zip File is closed"
提问by Purnima
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;
}
By running above code am getting error like this........
通过运行上面的代码我得到这样的错误........
Exception in thread "main" java.lang.IllegalStateException: Zip File is closed at org.apache.poi.openxml4j.util.ZipFileZipEntrySource.getEntries(ZipFileZipEntrySource.java:45) at 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) at 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) at ExcelReader.main(ExcelReader.java:30)
线程“main”中的异常 java.lang.IllegalStateException:Zip 文件在 org.apache.poi.openxml4j.util.ZipFileZipEntrySource.getEntries(ZipFileZipEntrySource.java:45) 处关闭,位于 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) 在 org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:294) 在 ExcelReader.readExcel(ExcelReader.java: 16) 在 ExcelReader.main(ExcelReader.java:30)
Can anyone help me tracing out what exactly is the problem.
任何人都可以帮我找出到底是什么问题。
I Googled but couldn't get the solution!
我用谷歌搜索但找不到解决方案!
回答by Vicky
To read an xslx file use create an object of FileInputStream class
要读取 xslx 文件,请使用创建 FileInputStream 类的对象
//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);
Hope this heps you...
希望这对你有帮助...
回答by Nick
Try shortening the file name and file path. Looks like there is a limit on how long the characters can be between " and ". It worked for me!
尝试缩短文件名和文件路径。看起来字符在“和”之间的长度是有限制的。它对我有用!
回答by jlm1150
You don't necessarily need to pass a FileInputStreamwhen creating a XSSFWorkbookobject, you can also pass an absolute path+filename as a String and it can be quite long (works for me with 101 characters, 108 characters with the double slashes, could probably be longer). I just wrote a small local app under Windows 7 whose only argument is a property file containing (among other properties) the absolute path+filename of the .xlsx file that I want to deal with (example property format : datasetFile=C:\\Users\\jlm\\Documents\\Test Cases\\AAAS\\TestCase2JsonGenerator\\AAAS_g1.xlsx
). I just pass the datasetFileproperty as parameter to the XSSFWorkbookconstructor (example code line : wb = new XSSFWorkbook(tags.get("datasetFile"));
). It works just fine, BUT don't forget any of the double slashes otherwise you get the "Zip File is closed" exception (about 2 hours lost).
在创建XSSFWorkbook对象时,您不一定需要传递FileInputStream,您也可以将绝对路径+文件名作为字符串传递,并且它可以很长(对我来说有 101 个字符,108 个带有双斜杠的字符,可以可能会更长)。我刚刚在 Windows 7 下编写了一个小型本地应用程序,它的唯一参数是一个属性文件,其中包含(以及其他属性)我想要处理的 .xlsx 文件的绝对路径+文件名(示例属性格式:)。我只是将datasetFile属性作为参数传递给XSSFWorkbook构造函数(示例代码行:datasetFile=C:\\Users\\jlm\\Documents\\Test Cases\\AAAS\\TestCase2JsonGenerator\\AAAS_g1.xlsx
wb = new XSSFWorkbook(tags.get("datasetFile"));
)。它工作得很好,但不要忘记任何双斜杠,否则你会得到“Zip 文件已关闭”异常(大约丢失 2 小时)。