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(); } }
excel对数据进行筛选和去重
最新推荐文章于 2024-06-16 21:31:37 发布