【导入】JAVA实现兼容不同版本excel文件的压缩包导入,读取数据到对应实体类

【xls是excel2003及以前版本生成的文件格式,而xlsx是excel2007及以后版本生成的文件格式】

一、场景

压缩包方式导入,需要兼容xlsx和xls的excel文件,文件流的方式作为入参

二、遇到问题

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

翻译一下:提供的数据似乎在Office 2007+XML中。您正在调用POI中处理OLE2 Office文档的部分。您需要调用POI的不同部分来处理这些数据(例如XSSFWorkbook代替HSSFWorkbook)

问题原因:上传文件类型和读取Workbook不对应【XSSFWorkbook用来读取xlsx类型的,HSSFWorkbook用来读取xls类型的】

三、解决办法

   List<ImportAddStoreEntity> myObjects = new ArrayList<>();

   try (ZipInputStream zis = new ZipInputStream(zisInputStream,Charset.forName("GBK"))) 
   {
    ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null) {
        // 获取Excel文件的输入流
        InputStream inputStream = zis;

        // 创建工作簿对象
        Workbook workbook = null;
        try {
            workbook = new XSSFWorkbook(inputStream);
        }catch (Exception ex){
            workbook = new HSSFWorkbook(inputStream);
        }
        
        //读取excel内容到实体类
        readExcelFile(workbook,myObjects);

        inputStream.close();
        zis.closeEntry();
    }
   }catch (IOException e) {
   }



   /**
     * 读取excel内容到实体类
     * @param workbook
     * @param myObjects
     * @throws Exception
   */
   private static void readExcelFile(Workbook workbook,List<ImportEntity> 
   myObjects) throws Exception 
   {
        // 获取第一个工作表
        Sheet sheetAt = workbook.getSheetAt(0);

        int rowNum = sheetAt.getLastRowNum();
        // 第一行为标题,从第二行开始读取
        for (int i = 2; i <= rowNum; i++) {
            Row row;
            row = sheetAt.getRow(i);
            if (row != null) {
                ImportEntity obj = createExcelEntity(row);
                myObjects.add(obj);
            }
        }

        // 关闭工作簿
        workbook.close();
    }



    /**
     * 获取excel行数据的每一列数据填充到实体类
     * @param row
    */
    private static ImportEntity createExcelEntity(Row row) {
        ImportEntity obj = new ImportEntity();

        DataFormatter dataFormatter = new DataFormatter();

        for (int j = 0; j < row.getLastCellNum(); j++) {
            Cell cell = row.getCell(j);
            String cellValue = dataFormatter.formatCellValue(cell);

            if(j == 0){
                obj.setStoreName(cellValue);
            }else if(j == 1){
                obj.setSelfStatusStr(cellValue);
            }else if(j == 2){
                obj.setCooperationStateStr(cellValue);
            }
        }
        return obj;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上花开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值