excel对数据进行筛选和去重

1.pom引入hutool的excel

2.选择一个字段作为key,具有唯一性
3.把一条为空的数据放在缓存,通过循环去不断地和新的数据做对比把符合条件的一条放缓存
4.对照代码改一改,快点跑起来~

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;

import java.util.*;

public class GONGSI {
    public static void main(String[] args) {
        ExcelReader reader = ExcelUtil.getReader("E:\\sheet1.xlsx");
        List<Map<String, Object>> readAll = reader.readAll();
        Iterator<Map<String, Object>> iterator = readAll.iterator();

        Map<String, Map<String, Object>> mapHashMap = new HashMap<>();

        while (iterator.hasNext()) {
            Map<String, Object> next = iterator.next();
            String eName = (String) next.get("企业名称");
            eName = eName.trim();
            Map<String, Object> entity = mapHashMap.get(eName);
            if (MapUtils.isEmpty(entity)) {
                mapHashMap.put(eName, next);
            } else {
                String codeLast = (String) entity.get("统一信用代码");
                String codeNext = (String) next.get("统一信用代码");

                if (StrUtil.isNotBlank(codeLast) && StrUtil.isBlank(codeNext)) {
                    next.put("统一信用代码", codeLast);
                }

                String isContractLast = (String) entity.get("是否签合同");
                String isContractNext = (String) next.get("是否签合同");
                if ("否".equals(isContractLast) && "是".equals(isContractNext)) {
                    mapHashMap.put(eName, next);
                }

                String sysLast = String.valueOf(entity.get("系统"));
                String sysNext = String.valueOf(next.get("系统"));
                if ("8910".equals(sysLast) && "7788".equals(sysNext)) {
                    mapHashMap.put(eName, next);
                }

            }
        }

        List<Map<String, Object>> list = new ArrayList<>();
        mapHashMap.entrySet().forEach(x -> {
            list.add(x.getValue());
        });


        ExcelWriter writer = ExcelUtil.getWriter("E:\\s2.xlsx");

        writer.passCurrentRow();

        writer.write(list, true);

        writer.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值