java实现Excel导入实战篇(迭代二)

java实现Excel导入实战篇(迭代二)_第1张图片
![导入Excel图.gif](http://upload-images.jianshu.io/upload_images/2284182-f9d6be930b4754aa.gif?imageMogr2/auto-orient/strip)

一、实现页面导入excel

    目录:1.配置文件 2.页面展示 3.后台代码 4.图片粘贴演示
1.配置文件

        码 ![收藏代码](http://upload-images.jianshu.io/upload_images/2284182-c724220fab33958c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

  
          
          
          
            
      
2.页面展示
2.1html代码


    
    
    
    
    
    Excel导入

    
    




注意事项:

1、通过此功能将外部渠道的信息导入到CLM系统。

2、导入的Excel不能隐藏列(如果有隐藏列会导致导入数据出现问题)。

3、目前只支持excel(以.xlsx或.xls结尾)文件导入,请下载Excel模板并严格按模板要求整理信息数据。excel模板下载

注释:如果需要增加列,请联系开发人员。


java实现Excel导入实战篇(迭代二)_第2张图片
图片.png
java实现Excel导入实战篇(迭代二)_第3张图片
图片.png
java实现Excel导入实战篇(迭代二)_第4张图片
图片.png
3.后台代码SpringMvc
 @RequestMapping(value = "/clmCustomer/excelImportView", method = { RequestMethod.GET })
    public String list(HttpServletRequest request, Model model, HttpServletResponse response) {
        return "/clmpool/excelImportView";//导出页面
    }
 /**
     * 导入excel数据。
     * 
     * @return
     */
    @RequestMapping(value = "/clmCustomer/excelImport", method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public ModelResult> excelImportData(@RequestParam("excelFile") MultipartFile multipartFile,
                                                            HttpServletRequest request, HttpServletResponse response) {

        ModelResult> result = new ModelResult>();
        List list = null;

        List excelImportModelList = Lists.newArrayList();
        // 所有问题数据返回
        Map problemMap = Maps.newConcurrentMap();
        List problemList = Lists.newArrayList();
        int count = 1;
        try {

            if (!multipartFile.isEmpty()) {
                // ====================== 这个就是导入的excel返回的JSON数据============================start
                list = ExcelImportUtil.exportListFromExcel(multipartFile, 0);

                LOG.info("ExcelImportUtil return listData:{}", JSON.toJSONString(list));
                System.out.println(JSON.toJSONString(list));
                // ====================== 这个就是导入的excel返回的JSON数据============================end

                // =======================解析json进行业务 处理=========================================start
                for (String string : list) {
                    count++;
                    String[] split = string.split("=");
                    System.out.println(split.length);
                    ExcelImportModel importModel = new ExcelImportModel();
                    importModel.setCompanyName(split[1]);
                    importModel.setContactPerson(split[2] + split[3].replace("未填写", ""));
                    importModel.setMobile(split[4]);
                    StringBuffer remark = new StringBuffer();
                    if ((split.length > 6) && StringUtils.isNotBlank(split[5])) {
                        if (StringUtils.isNotBlank(split[5].replace("\\N", ""))) {
                            remark.append("座机:" + split[5].replace("\\N", ""));
                        }
                    }
                    if ((split.length > 7) && StringUtils.isNotBlank(split[6])) {
                        if (StringUtils.isNotBlank(split[6].replace("\\N", ""))) {
                            remark.append(";邮箱:" + split[6].replace("\\N", ""));
                        }
                    }
                    if ((split.length > 8) && StringUtils.isNotBlank(split[7])) {
                        if (StringUtils.isNotBlank(split[7].replace("\\N", ""))) {
                            remark.append(";传真:" + split[7].replace("\\N", ""));
                        }
                    }
                    if ((split.length > 9) && StringUtils.isNotBlank(split[8])) {
                        if (StringUtils.isNotBlank(split[8].replace("\\N", ""))) {
                            remark.append(";公司介绍:" + split[8].replace("\\N", ""));
                        }
                    }
                    if (StringUtils.isNotBlank(remark.toString())) {
                        importModel.setRemark(remark.toString());
                    }
                    if ((split.length > 10) && StringUtils.isNotBlank(split[9])) {
                        importModel.setBusinessProvince(split[9].replace("省", ""));
                    }
                    if ((split.length > 11) && StringUtils.isNotBlank(split[10])) {
                        importModel.setBusinessCity(split[10].replace("市", ""));
                    }
                    if ((split.length > 12) && StringUtils.isNotBlank(split[11])) {
                        importModel.setBusinessAddress(split[11]);
                    }
                    if ((split.length > 13) && StringUtils.isNotBlank(split[12])) {
                        importModel.setMainProd("|"
                                                + split[12].trim().replace(",", "|").replace(",", "|").replace(" ", "")
                                                + "|");
                    }
                    excelImportModelList.add(importModel);
                }

                // =======================解析json进行业务 处理=========================================end
                LOG.info("excelImportModelList:{}", JSON.toJSONString(excelImportModelList));
                // 标签(来源)id
                Long tagId = getTagId();
                if (tagId > 0) {
                    for (ExcelImportModel excelImportModel : excelImportModelList) {
                        // 校验手机号
                        Map checkPhoneNumberBelongs = PhoneNumberBelongsUtil.checkPhoneNumberBelongs(excelImportModel.getMobile());
                        if (CollectionUtils.isEmpty(checkPhoneNumberBelongs)
                            || StringUtils.isEmpty(excelImportModel.getCompanyName())
                            || StringUtils.isEmpty(excelImportModel.getCompanyName().replace("\\N", ""))) {
                            ExcelImportModel newexcelImportModel = new ExcelImportModel();
                            newexcelImportModel.setCompanyName(excelImportModel.getCompanyName());
                            newexcelImportModel.setMobile(excelImportModel.getMobile());
                            problemList.add(newexcelImportModel);
                            continue;
                        }
                        // 1.根据手机号判断是否有会员或者是否已经注册客户。
                        if (StringUtils.isNotBlank(excelImportModel.getMobile())) {
                            ClmPoolParam clmPoolParam = new ClmPoolParam();
                            clmPoolParam.setMobile(excelImportModel.getMobile());
                            ModelListResult queryClmPoolModel = clmPoolQueryService.query(clmPoolParam);
                            if (queryClmPoolModel != null && CollectionUtils.isNotEmpty(queryClmPoolModel.getModel())) {
                                continue;
                            }
                        } else {
                            // 如果手机号为null,调过该条数据
                            ExcelImportModel newexcelImportModel = new ExcelImportModel();
                            newexcelImportModel.setCompanyName(excelImportModel.getCompanyName());
                            newexcelImportModel.setMobile(excelImportModel.getMobile());
                            problemList.add(newexcelImportModel);
                            continue;
                        }

                        // 2.判断是否区域为null,如果不为null,则根据区域判断分池。
                        short poolStatus = 0;
                        if (StringUtils.isNotBlank(excelImportModel.getBusinessProvince())) {
                            poolStatus = ClmBaseMessageUtil.getPoolStatusByBelongsArea(excelImportModel.getBusinessProvince());
                            if (poolStatus > 0) {
                                insertClmPool(excelImportModel, tagId, poolStatus,
                                              excelImportModel.getBusinessProvince(),
                                              excelImportModel.getBusinessCity());
                                continue;
                            }
                        }

                        // 3.根据手机号识别区域和补充信息。
                        poolStatus = ClmBaseMessageUtil.getPoolStatusByMobile(excelImportModel.getMobile());
                        if (poolStatus > 0) {
                            insertClmPool(excelImportModel, tagId, poolStatus, excelImportModel.getBusinessProvince(),
                                          excelImportModel.getBusinessCity());
                        } else {
                            ExcelImportModel newexcelImportModel = new ExcelImportModel();
                            newexcelImportModel.setCompanyName(excelImportModel.getCompanyName());
                            newexcelImportModel.setMobile(excelImportModel.getMobile());
                            problemList.add(newexcelImportModel);
                        }

                    }
                }

            }
            // 总池中不存在则创建
            problemMap.put("problemData", JSON.toJSONString(problemList));
            problemMap.put("problemCount", problemList.size());
            problemMap.put("totalCount", excelImportModelList.size());
            result.setModel(problemMap);
            LOG.info("problemData:{},problemCount:{}", JSON.toJSONString(problemList), problemList.size());

        } catch (Exception e) {
            System.out.println("import excel error 第" + count + "数据有问题");
            LOG.info("import excel error :{},count:{}", e, count);
            result.setErrorMessage(ResultCode.INSERT_10100.getCode(),
                                   "import excel error please check import excel data problem!第" + count + "数据有问题");
        }
        return result;
    }

4.动态图片操作演示
导入Excel图.gif

你可能感兴趣的:(java实现Excel导入实战篇(迭代二))