通用导出模板

protected Result importExcel(HttpServletRequest request, HttpServletResponse response, Class clazz) {
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    Map fileMap = multipartRequest.getFileMap();
    for (Map.Entry entity : fileMap.entrySet()) {
        // 获取上传文件对象
        MultipartFile file = entity.getValue();
        ImportParams params = new ImportParams();
        params.setTitleRows(2);
        params.setHeadRows(1);
        params.setNeedSave(true);
        try {
            List 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 result = new ArrayList();
      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 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);
   }

你可能感兴趣的:(java,服务器,windows)