通用导出模板

复制代码
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);
   }
相关推荐
南尘NCA86662 小时前
企业微信防封防投诉拦截系统:从痛点解决到技术实现
java·网络·企业微信
zhuyasen3 小时前
踩坑实录:Go 1.25.x 编译的 exe 在 Windows 提示“此应用无法运行”
windows·golang
板鸭〈小号〉3 小时前
Socket网络编程(2)-command_server
运维·服务器
怪兽20143 小时前
SQL优化手段有哪些
java·数据库·面试
ss2733 小时前
手写MyBatis第107弹:@MapperScan原理与SqlSessionTemplate线程安全机制
java·开发语言·后端·mybatis
千歌叹尽执夏4 小时前
ubuntu24.04lts和Windows11家庭版远程桌面连接若干问题(解决)
windows·远程连接·xrdp·ubuntu24.04lts
Deschen4 小时前
设计模式-原型模式
java·设计模式·原型模式
麦麦鸡腿堡4 小时前
Java的动态绑定机制(重要)
java·开发语言·算法
それども4 小时前
SpringBootTest运行线程池被拒绝
java
介一安全5 小时前
【Frida Android】基础篇6:Java层Hook基础——创建类实例、方法重载、搜索运行时实例
android·java·网络安全·逆向·安全性测试·frida