通用导出模板

复制代码
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);
   }
相关推荐
程序员JerrySUN几秒前
[特殊字符] 深入理解 Linux 内核进程管理:架构、核心函数与调度机制
java·linux·架构
2302_809798324 分钟前
【JavaWeb】Docker项目部署
java·运维·后端·青少年编程·docker·容器
玩转4G物联网8 分钟前
零基础玩转物联网-串口转以太网模块如何快速实现与TCP服务器通信
服务器·网络·物联网·网络协议·tcp/ip·http·fs100p
派阿喵搞电子26 分钟前
Ubuntu下有关UDP网络通信的指令
linux·服务器·网络
网安INF27 分钟前
CVE-2020-17519源码分析与漏洞复现(Flink 任意文件读取)
java·web安全·网络安全·flink·漏洞
一叶知秋哈28 分钟前
Java应用Flink CDC监听MySQL数据变动内容输出到控制台
java·mysql·flink
jackson凌33 分钟前
【Java学习笔记】SringBuffer类(重点)
java·笔记·学习
sclibingqing39 分钟前
SpringBoot项目接口集中测试方法及实现
java·spring boot·后端
程序员JerrySUN43 分钟前
全面理解 Linux 内核性能问题:分类、实战与调优策略
java·linux·运维·服务器·单片机
糯米导航1 小时前
Java毕业设计:办公自动化系统的设计与实现
java·开发语言·课程设计