Reputation: 1174
I am using Apache POI for reading xsl files. My pom file update:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.0</version>
</dependency>
My code:
public List<String> getSheetNames(String fileLocation) throws Exception {
FileInputStream file = new FileInputStream(new File(fileLocation));
Workbook workbook = null;
try {
workbook = new XSSFWorkbook(file);
} catch (Exception e) {
e.printStackTrace();
}
//do something with read lines
}
This works fine for some files. However, for others it throws this error:
org.apache.commons.compress.archivers.zip.UnsupportedZipFeatureException: Unsupported feature compression method used in entry ...
...
at org.apache.commons.compress.archivers.zip.ZipUtil.checkRequestedFeatures(ZipUtil.java:353)
at org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.read(ZipArchiveInputStream.java:514)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.read(ZipArchiveThresholdInputStream.java:80)
at org.apache.poi.util.IOUtils.toByteArray(IOUtils.java:185)
at org.apache.poi.util.IOUtils.toByteArray(IOUtils.java:153)
at org.apache.poi.util.IOUtils.toByteArray(IOUtils.java:140)
at org.apache.poi.openxml4j.util.ZipArchiveFakeEntry.<init>(ZipArchiveFakeEntry.java:72)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:98)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:132)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:312)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:59)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:304)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:300)
what's the fix for this error? I haven't been able to find much help online.
UPDATE: This is not working for any file now.
Upvotes: 1
Views: 597
Reputation: 49
Make the changes like this. if the file is of type xls, you have to use HSSFWorkbook obj to read it
public List<String> getSheetNames(String fileLocation) throws Exception{
File inputFile = new File(fileLocation);
FileInputStream fis = new FileInputStream(inputFile);
Workbook workbook = null;
try {
if (inputFile.getName().endsWith(".xlsx")) {
workbook = new XSSFWorkbook(fis);
} else if (inputFile.getName().endsWith(".xls")) {
workbook = new HSSFWorkbook(fis);
} else{
fis.close();
throw new IOException("This file extension type is not supported!");
}
} catch (Exception e) {
e.printStackTrace();
}
//do something with read lines
}
Upvotes: 1