TSwanson9411
TSwanson9411

Reputation: 21

Apache POI handles .xls but throws exception on .xlsx

I am using NetBeans 14 to build a Java 8 project with Ant build. I have included:

poi-5.2.3.jar
poi-ooxml-full-5.2.3.jar
SpareseBitSet-1.2.jar
commons-codec-1.15.jar
commons-collections4-4.4.jar
commons-compress-1.2.1.jar
commons-io-2.11.0.jar
commons-math3-3.6.1.jar
curvesapi-1.0.7.jar
xml-api-1.4.0.1.jar
xmlbeans-5.1.1.jar

It builds fine. When I create a Workbook using an xls file as input it works. When I try to create a Workbook using an xlsx file it throws an exception reporting:

IO Exception processing input Bio file
java.io.IOException: Your InputStream was neither an OLE2 stream, nor an OOXML stream or you haven't provide the poi-ooxml*.jar in the classpath/modulepath - FileMagic: OOXML, having providers: [org.apache.poi.hssf.usermodel.HSSFWorkbookFactory@71a2f010]
    at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:334) ~[poi-5.2.3.jar:5.2.3]
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:311) ~[poi-5.2.3.jar:5.2.3]
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:277) ~[poi-5.2.3.jar:5.2.3]
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:255) ~[poi-5.2.3.jar:5.2.3]

I've tried changing ooxml library types (-lite, -full, both, etc.). Any help would be welcome.

Upvotes: 1

Views: 988

Answers (1)

Gyanendra Dwivedi
Gyanendra Dwivedi

Reputation: 5557

I have tried sample code, working fine.

File myFile = new File("C://temp/Employee.xlsx"); FileInputStream fis = new 

FileInputStream(myFile); 

// Finds the workbook instance for XLSX file 
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis); 

// Return first sheet from the XLSX workbook 
XSSFSheet mySheet = myWorkBook.getSheetAt(0); 

// Get iterator to all the rows in current 
sheet Iterator<Row> rowIterator = mySheet.iterator(); 

// Traversing over each row of XLSX file 
while (rowIterator.hasNext()) { 
    
    Row row = rowIterator.next(); 
    // For each row, iterate through each columns 
    
    Iterator<Cell> cellIterator = row.cellIterator(); 
        while (cellIterator.hasNext()) { 
            Cell cell = cellIterator.next(); 
            switch (cell.getCellType()) { 
                // Enter you code here to fetch data based on cell type
                
                } 
            } System.out.println(""); 
        }

Upvotes: 1

Related Questions