easyExcle导入验证表格内容有多少是重复的(包含获取集合重复数据和根据某些字段去重方法)

该代码段展示了如何使用SpringBoot控制器处理员工列表的导入,通过MultipartFile接收文件,利用EasyExcel库读取Excel数据并进行重复值检查。服务层读取Excel后,检查身份证号和手机号的重复项,并进行去重处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

controller

    @ApiOperation("导入员工列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "导入文件", dataType = "java.io.File", required = true),
    })
    @Log(title = "员工管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport, String departId) throws Exception {
        updateSupport=true;
        List<String> names=iEntStaffService.readExcel(file);
        ExcelResult<EntStaffImportVo> result = ExcelUtil.importExcel(file.getInputStream(), EntStaffImportVo.class, new EntStaffImportListener(updateSupport,departId,names));
        return R.ok(result.getAnalysis());
    }

service

    public List<String> readExcel(MultipartFile file) throws IOException {
        List<String> names=new ArrayList<>();
        // 获取输入流,将 Excel 文件读取出来
        InputStream inputStream = file.getInputStream();
        List<EntStaffImportVo> entStaffImportVoList = EasyExcel.read(inputStream)
                // 设置与Excel表映射的类
                .head(EntStaffImportVo.class)
                // 设置sheet,默认读取第一个
                .sheet()
                // 设置标题所在行数
                .headRowNumber(1)
                // 异步读取
                .doReadSync();
        if(entStaffImportVoList!=null&&entStaffImportVoList.size()>0){
            List<EntStaffImportVo> resList=new ArrayList<>();
            List<EntStaffImportVo> results = HashMultiset.create(entStaffImportVoList).entrySet().stream()
                    .filter(w -> w.getCount() > 1)
                    .map(Multiset.Entry::getElement)
                    .collect(Collectors.toList());
            // 取出来重复身份证号的
            List<String> idcardList = entStaffImportVoList.stream().collect(Collectors.groupingBy(EntStaffImportVo::getIdcard, Collectors.counting()))
                    .entrySet().stream().filter(e -> e.getValue() > 1)
                    .map(Map.Entry::getKey).collect(Collectors.toList());
            // 取出来重复手机号的
            List<String> phoneList = entStaffImportVoList.stream().collect(Collectors.groupingBy(EntStaffImportVo::getPhone, Collectors.counting()))
                    .entrySet().stream().filter(e -> e.getValue() > 1)
                    .map(Map.Entry::getKey).collect(Collectors.toList());
            for(EntStaffImportVo entStaffImportVo:entStaffImportVoList){
                if(idcardList.contains(entStaffImportVo.getIdcard())||phoneList.contains(entStaffImportVo.getPhone())){
                    resList.add(entStaffImportVo);
                }
            }

            //根据身份号码和手机号去重
            ArrayList<EntStaffImportVo> paperRecordList = resList.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(item->item.getIdcard()+"_"+item.getPhone()+"_"+item.getStaffName()))), ArrayList::new)
            );
            if(paperRecordList!=null&&paperRecordList.size()>0){
                for (EntStaffImportVo entStaffImportVo:paperRecordList){
                    names.add(entStaffImportVo.getStaffName());
                }
            }
        }
        return names;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值