JAVA:excel导入后解析报错

org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)

解决办法分别使用hssf与xssf解析03(xls)和07(xlsx)的文件

        String extName = "."+FilenameUtils.getExtension(file.getName());

        Workbook wb = null;
        if (ExcelVersion.V2003.getSuffix().equals(extName)) {
            wb = new HSSFWorkbook(new FileInputStream(file));

        } else if (ExcelVersion.V2007.getSuffix().equals(extName)) {
            wb = new XSSFWorkbook(new FileInputStream(file));

        } else {
            // 无效后缀名称,这里之能保证excel的后缀名称,不能保证文件类型正确,不过没关系,在创建Workbook的时候会校验文件格式
            throw new IllegalArgumentException("Invalid excel version");
        }

猜你喜欢

转载自blog.csdn.net/u011374582/article/details/83270016