Vy Do
Vy Do

Reputation: 52656

Error: Type mismatch: cannot convert from HSSFWorkbook to Workbook

I use poi-3.2-FINAL-20081019.jar . Error:

Type mismatch: cannot convert from HSSFWorkbook to Workbook

try {
            if (strType.equals("xls")) {
                wb = new HSSFWorkbook(inputStream);
            } else {
                wb = new XSSFWorkbook(inputStream);
            }
            Sheet sheet = wb.getSheetAt(0);

How to fix it?

Upvotes: 0

Views: 1634

Answers (2)

Gagravarr
Gagravarr

Reputation: 48376

As the date in your jar shows - poi-3.2-FINAL-20081019.jar - you're usigng a jar that's almost 10 years old! You need to upgrade to something more modern, and at the very least something from this decade...

Right now (November 2017), the latest version is Apache POI 3.17. You can find the latest version on the Apache POI homepage, and see all the fixes in the Changelog

In addition, you should swap to using WorkbookFactory rather than looking at file extensions to work out what class to use. That hides all the detection complexity for you, works around mis-named files etc

Your code can then become the very simple

Workbook wb = WorkbookFactory.create(new File("input.xlsx"));
Sheet s = wb.getSheetAt(0);

(Use a File if you can, rather than InputStream, for lower memory)

Upvotes: 1

Related Questions