0

I'm developing an eclipse based Java application. But I can't catch the exception. I want to read an excel file(.xlsx) using HSSF and XSSF simultaneously. But HSSF works properly otherwise XSSF doesn't. Here is my code. After wb = new XSSFWorkbook(pkg); statement the program count goes to finally statement. Thanks in advance.

 try {
        if (FileFormat == FILE_FORMAT_XLS) {
            fs = new POIFSFileSystem(new FileInputStream(szFileName));
            wb = new HSSFWorkbook(fs);
        }
        // This block does not work properly.
        else if (FileFormat == FILE_FORMAT_XLSX) {
            file = new File(szFileName);
            if (file.exists() == false || file.isFile() == false || file.canRead() == false) {
                throw new IOException(szFileName);
            }

            pkg = OPCPackage.open(file);
            wb = new XSSFWorkbook(pkg); // **<- Here is the problem.**
        }

        if (wb != null) {
            evaluator = wb.getCreationHelper().createFormulaEvaluator();
            sheetNum = wb.getNumberOfSheets();
            for (int i = 0; i < sheetNum; i++) {
                for (Row row : wb.getSheetAt(i)) {
                    person ps = new person();
                    if (getPerson(row, ps) == true) {
                        list.add((Object) ps);
                        log.addLog(list.size() + " " + ps.getName() + ", " + ps.getHanName() + ", " + ps.getEnName() + ", " + ps.getBirthDay() + ", " + ps.getCellPhone() + ", " + ps.getID() + ", " + ps.getAddress());
                    } else {
                        log.addLog("total count = " + list.size());
                        ps = null;
                        break;
                    }
                }
            }
        } else {
            log.addLog("can not open workbook");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        if (wb != null) {
            wb = null;
        }
    }
SANN3
  • 9,459
  • 6
  • 61
  • 97

1 Answers1

0

Does the program go to finally without catching the exception? Either the wb object is null or there will be an exception stack trace.

Also the wb object has to be of appropriate type. Either HSSF or XSSF. Try instantiating the xssfworkbook like

XSSFWorkbook workbook = new XSSFWorkbook(szFileName);