通用导出模板

protected Result<?> importExcel(HttpServletRequest request, HttpServletResponse response, Class<T> clazz) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
    for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
        // 获取上传文件对象
        MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(2);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
            //update-begin-author:taoyan date:20190528 for:批量插入数据
            long start = System.currentTimeMillis();
            service.saveBatch(list);
            //400条 saveBatch消耗时间1592毫秒  循环插入消耗时间1947毫秒
            //1200条  saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
            log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
            //update-end-author:taoyan date:20190528 for:批量插入数据
            return Result.ok("文件导入成功!数据行数:" + list.size());
        } catch (Exception e) {
            //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
            String msg = e.getMessage();
            log.error(msg, e);
            if(msg!=null && msg.indexOf("Duplicate entry")>=0){
                return Result.error("文件导入失败:有重复数据!");
            }else{
                return Result.error("文件导入失败:" + e.getMessage());
            }
            //update-end-author:taoyan date:20211124 for: 导入数据重复增加提示
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return Result.error("文件导入失败!");
}
     */
   public ExcelImportResult importExcelByIs(InputStream inputstream, Class<?> pojoClass, ImportParams params) throws Exception {
      if (LOGGER.isDebugEnabled()) {
         LOGGER.debug("Excel import start ,class is {}", pojoClass);
      }
      List<T> result = new ArrayList<T>();
      Workbook book = null;
      boolean isXSSFWorkbook = false;
      if (!(inputstream.markSupported())) {
         inputstream = new PushbackInputStream(inputstream, 8);
      }
      //begin-------author:liusq------date:20210129-----for:-------poi3升级到4兼容改造工作【重要敏感修改点】--------
      //------poi4.x begin----
//    FileMagic fm = FileMagic.valueOf(FileMagic.prepareToCheckMagic(inputstream));
//    if(FileMagic.OLE2 == fm){
//       isXSSFWorkbook=false;
//    }
      book = WorkbookFactory.create(inputstream);
      if(book instanceof XSSFWorkbook){
         isXSSFWorkbook=true;
      }
      LOGGER.info("  >>>  poi3升级到4.0兼容改造工作, isXSSFWorkbook = " +isXSSFWorkbook);
      //end-------author:liusq------date:20210129-----for:-------poi3升级到4兼容改造工作【重要敏感修改点】--------

      //begin-------author:liusq------date:20210313-----for:-------多sheet导入改造点--------
      //获取导入文本的sheet数
      //update-begin-author:taoyan date:20211210 for:https://gitee.com/jeecg/jeecg-boot/issues/I45C32 导入空白sheet报错
      if(params.getSheetNum()==0){
         int sheetNum = book.getNumberOfSheets();
         if(sheetNum>0){
            params.setSheetNum(sheetNum);
         }
      }
      //update-end-author:taoyan date:20211210 for:https://gitee.com/jeecg/jeecg-boot/issues/I45C32 导入空白sheet报错
      //end-------author:liusq------date:20210313-----for:-------多sheet导入改造点--------
      createErrorCellStyle(book);
      Map<String, PictureData> pictures;

      //update-begin-author:liusq date:20220609 for:issues/I57UPC excel导入 ImportParams 中没有startSheetIndex参数
      for (int i = params.getStartSheetIndex(); i < params.getStartSheetIndex()
            + params.getSheetNum(); i++) {
      //update-end-author:liusq date:20220609 for:issues/I57UPC excel导入 ImportParams 中没有startSheetIndex参数
         if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(" start to read excel by is ,startTime is {}", System.currentTimeMillis());
         }
         if (isXSSFWorkbook) {
            pictures = PoiPublicUtil.getSheetPictrues07((XSSFSheet) book.getSheetAt(i), (XSSFWorkbook) book);
         } else {
            pictures = PoiPublicUtil.getSheetPictrues03((HSSFSheet) book.getSheetAt(i), (HSSFWorkbook) book);
         }
         if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(" end to read excel by is ,endTime is {}", new Date().getTime());
         }
         result.addAll(importExcel(result, book.getSheetAt(i), pojoClass, params, pictures));
         if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(" end to read excel list by pos ,endTime is {}", new Date().getTime());
         }
      }
      if (params.isNeedSave()) {
         saveThisExcel(params, pojoClass, isXSSFWorkbook, book);
      }
      return new ExcelImportResult(result, verfiyFail, book);
   }

相关推荐

  1. 通用导出模板

    2024-01-19 11:22:05       46 阅读
  2. EasyExcel 通过模板 导入导出、下载模板

    2024-01-19 11:22:05       51 阅读
  3. 前端模块导入导出方式

    2024-01-19 11:22:05       30 阅读
  4. 通过easyExcel实现表格的导入导出

    2024-01-19 11:22:05       70 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-19 11:22:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-19 11:22:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-19 11:22:05       82 阅读
  4. Python语言-面向对象

    2024-01-19 11:22:05       91 阅读

热门阅读

  1. C#设计模式教程(4):单例模式

    2024-01-19 11:22:05       50 阅读
  2. 1.7 面试经典150题 - H指数

    2024-01-19 11:22:05       59 阅读
  3. arcgis js 4.x加载地图服务跨域配置(.Net方式)

    2024-01-19 11:22:05       51 阅读
  4. 【CSS】垂直居中的四种实现方式

    2024-01-19 11:22:05       56 阅读
  5. 离线安装python2的MySQLdb

    2024-01-19 11:22:05       51 阅读
  6. LeetCode解法汇总2171. 拿出最少数目的魔法豆

    2024-01-19 11:22:05       50 阅读
  7. 网络工程师:软件编程基础知识面试题(九)

    2024-01-19 11:22:05       50 阅读
  8. Python爬虫案例分享

    2024-01-19 11:22:05       55 阅读
  9. 前端为什么要使用枚举?(优化``if-else``版)

    2024-01-19 11:22:05       50 阅读
  10. kafka入门(八):副本

    2024-01-19 11:22:05       48 阅读
  11. Python:正则表达式之re.group()用法

    2024-01-19 11:22:05       51 阅读
  12. postgreSQL之grant

    2024-01-19 11:22:05       45 阅读