通用导出模板

复制代码
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);
   }
相关推荐
雷神乐乐9 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|14 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
逊嘘33 分钟前
【Java语言】抽象类与接口
java·开发语言·jvm
morris13140 分钟前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
龙哥说跨境1 小时前
如何利用指纹浏览器爬虫绕过Cloudflare的防护?
服务器·网络·python·网络爬虫
七星静香1 小时前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员1 小时前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU1 小时前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea
stewie61 小时前
在IDEA中使用Git
java·git
Elaine2023911 小时前
06 网络编程基础
java·网络