关注JEECG发展历程 关注最新动态和版本, 记录JEECG成长点滴 更新日志 - 技术支持 - 招聘英才

JEECG最新版本下载 JEECG智能开发平台 - 显著提高开发效率 常见问题 - 入门视频 - 参与开源团队

商务QQ: 69893005、3102411850 商务热线(5*8小时): 010-64808099 官方邮箱: jeecgos@163.com

查看: 8165|回复: 2

[EasyPOI] Excel导入一对多数据,没有实体,以map形式怎么做?

[复制链接]
发表于 2018-9-11 10:45:04 | 显示全部楼层 |阅读模式
//这是3.7.1的代码 在功能测试里导入一对多数据不能实现
@RequestMapping
(params = "importExcel", method = RequestMethod.POST)
@ResponseBody
@SuppressWarnings("all")
public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
   String message = "上传成功";
   AjaxJson j = new AjaxJson();
   String configId = request.getParameter("tableName");
   String jversion = cgFormFieldService.getCgFormVersionByTableName(configId);
   Map<String, Object> configs = configService.queryConfigs(configId, jversion);
   //数据库中版本号
   String version = (String) configs.get(CgAutoListConstant.CONFIG_VERSION);
   List<CgFormFieldEntity> lists = (List<CgFormFieldEntity>) configs.get(CgAutoListConstant.FILEDS);

   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
   Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
   for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
      MultipartFile file = entity.getValue();// 获取上传文件对象
      //上传文件的版本号
      String docVersion = getDocVersion(file.getOriginalFilename());
      if (docVersion.equals(version)) {
         List<Map<String, Object>> listDate;
         //--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
         try {
            //读取excel模版数据
            //--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
            ImportParams params = new ImportParams();
            params.setDataHanlder(new CgFormExcelHandler(lists));
            listDate = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
            //--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
            if (listDate == null) {
               message = "识别模版数据错误";
               logger.error(message);
            } else {
               //--author:zhoujf---start------date:20170207--------fornline表单物理表查询数据异常处理
               configId = configId.split("__")[0];
               for (Map<String, Object> map : listDate) {
                  map.put("id", UUIDGenerator.generate());
                  dataBaseService.insertTable(configId, map);
               }
               message = "文件导入成功!";
            }
         } catch (Exception e) {
            message = "文件导入失败!";
            logger.error(ExceptionUtil.getExceptionMessage(e));
         }
         //--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
      } else {
         message = "模版文件版本和表达不匹配,请重新下载模版";
         logger.error(message);
      }
   }
   j.setMsg(message);
   return j;
}
//这是3.7.3的代码 在功能测试里导入一对多数据不能实现
@RequestMapping(params = "importExcel", method = RequestMethod.POST)
@ResponseBody
@SuppressWarnings("all")
public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
   String message = "上传成功";
   AjaxJson j = new AjaxJson();
   String configId = request.getParameter("tableName");
   String jversion = cgFormFieldService.getCgFormVersionByTableName(configId);
   Map<String, Object> configs = configService.queryConfigs(configId, jversion);
   //数据库中版本号
   String version = (String) configs.get(CgAutoListConstant.CONFIG_VERSION);
   List<CgFormFieldEntity> lists = (List<CgFormFieldEntity>) configs.get(CgAutoListConstant.FILEDS);
   Object subTables = configs.get("subTables");
   List<String> subTabList = new ArrayList();
   if (null != subTables) {
      subTabList.addAll(Arrays.asList(subTables.toString().split(",")));
   }
   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
   Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
   for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
      MultipartFile file = entity.getValue();// 获取上传文件对象
      //上传文件的版本号
      String docVersion = getDocVersion(file.getOriginalFilename());
      if (docVersion.equals(version)) {
         List<Map<String, Object>> listDate;
         //--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
         try {
            //读取excel模版数据
            //--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
            ImportParams params = new ImportParams();
            params.setDataHanlder(new CgFormExcelHandler(lists));
            listDate = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
            //--author:JueYue---------date:20150622--------for: 修改为EasyPoi 的导入
            if (listDate == null) {
               message = "识别模版数据错误";
               logger.error(message);
            } else {
               //--author:zhoujf---start------date:20170207--------fornline表单物理表查询数据异常处理
               configId = configId.split("__")[0];

               String mainId = "";
               for (Map<String, Object> map : listDate) {
                  //标志是否为主表数据
                  boolean isMainData = false;
                  Set<String> keySet = map.keySet();
                  Map mainData = new HashMap();
                  for (String key : keySet) {
                     if (key.indexOf("$subTable$")==-1) {
                        if (key.indexOf("$mainTable$")!=-1 && StringUtils.isNotEmpty(map.get(key).toString())) {
                           isMainData = true;
                           mainId = UUIDGenerator.generate();
                        }
                        mainData.put(key.replace("$mainTable$", ""), map.get(key));
                     }
                  }
                  map.put("$mainTable$id", mainId);//为子表准备
                  if (isMainData) {

                     //处理字典项
                     dealDicForImport(mainData, lists);

                     mainData.put("id", mainId);//主表数据
                     dataBaseService.insertTable(configId, mainData);                           
                  }
               }
               //导入子表数据,如果有
               for (String subConfigId: subTabList) {
                  Map<String, Object> subConfigs = configService.queryConfigs(subConfigId, jversion);
                  List<CgFormFieldEntity> subLists = (List<CgFormFieldEntity>) subConfigs.get(CgAutoListConstant.FILEDS);
                  //将表头字段替换成数据库中对应的字段名
                  String configName = subConfigs.get("config_name").toString();
                  for (Map<String, Object> map : listDate) {
                     //标志是否为子表数据
                     boolean isSubData = false;
                     Map subData = new HashMap();
                     for (CgFormFieldEntity fieldEntity: subLists) {
                        String mainTab = fieldEntity.getMainTable();
                        String mainField = fieldEntity.getMainField();
                        boolean isForeignKey = configId.equals(mainTab) && StringUtil.isNotEmpty(mainField);
                        String tempKey = configName+"_"+fieldEntity.getContent();
                        //已经考虑兼容多个外键的情况
                        if(isForeignKey){
                           subData.put(fieldEntity.getFieldName(), map.get("$mainTable$"+mainField));
                        }
                        Object subObj = map.get("$subTable$"+tempKey);
                        if (null != subObj && StringUtils.isNotEmpty(subObj.toString())) {
                           isSubData = true;
                           //System.out.println(tempKey+"=>"+fieldEntity.getFieldName()+"--"+subObj);
                           subData.put(fieldEntity.getFieldName(), subObj);
                        }
                     }
                     //设置子表记录ID
                     if (isSubData) {

                        //处理字典项
                        dealDicForImport(subData, subLists);

                        subData.put("id", UUIDGenerator.generate());
                        dataBaseService.insertTable(subConfigId, subData);
                     }
                  }
               }

               message = "文件导入成功!";
            }
         } catch (Exception e) {
            message = "文件导入失败!";
            logger.error(ExceptionUtil.getExceptionMessage(e));
         }
         //--author:luobaoli---------date:20150615--------for: 处理service层抛出的异常
      } else {
         message = "模版文件版本和表达不匹配,请重新下载模版";
         logger.error(message);
      }
   }
   j.setMsg(message);
   return j;
}

//这是我的代码
List<TempFieldEntity> fields = new ArrayList<>();
        tempDataService.findTempFields(fields, tableName, import_temp, import_temp0, null);

        AjaxJson j = new AjaxJson();
        String message = null;
        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();
            List<Map<String, Object>> listData;
            try {
                List<String> fieldNames = new ArrayList<>();
                tempDataService.getFieldNameList(fieldNames, tableName, import_temp);

                CcFormExcelHandler excelHandler = new CcFormExcelHandler(fields);
                Map linkmanMap = new HashMap<>();
                if (tableName.equals(CUSTOMER_TABLE)) {
linkmanMap .put(mutiLangService.getLang("common.surname"), "fullName");
linkmanMap .put(mutiLangService.getLang("common.sex"), "sex");
linkmanMap .put(mutiLangService.getLang("common.mobile"), "telephone");
linkmanMap .put(mutiLangService.getLang("phone.belongto.place"), "mobileAttribution");
linkmanMap .put(mutiLangService.getLang("common.position"), "post");
linkmanMap .put(mutiLangService.getLang("common.mail"), "email");
linkmanMap .put("QQ", "qq");
linkmanMap .put(mutiLangService.getLang("common.address"), "proCityArea");
linkmanMap .put(mutiLangService.getLang("lky.detailed.address"), "address");
                     // 这段自己写的,报错了
                    Map map = new HashMap();
                    map.put("联系人",linkmanMap);
                    excelHandler.addMap(map);
                     // 如果这样是一对一的
                    //excelHandler.addMap(linkmanMap);
                }

                params.setDataHanlder(excelHandler);
                listData = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
                if (listData == null) {
                    message = "识别模版数据错误";
                    logger.error(message);
                } else {
////////////////////////////////////////////////////////////////////////////
求指教如何用map方式导入一对多数据????!!!!!!!!!!!

发表于 2018-9-11 13:19:58 | 显示全部楼层
不支持
 楼主| 发表于 2018-9-11 14:02:58 | 显示全部楼层
admin 发表于 2018-9-11 13:19
不支持

好的
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表