EasyExcel复杂表头数据导入

目录

表头示例

导入代码

java 复制代码
    @Override
    public void importExcel(InputStream inputStream) {
        ItemExcelListener itemExcelListener = new ItemExcelListener();
        EasyExcel.read(inputStream, ImportItem.class, itemExcelListener).headRowNumber(2).sheet().doRead();
    }
java 复制代码
@Slf4j
public class ItemExcelListener extends AnalysisEventListener<ImportItem> {
    /**
     * 定义100条数据存储一次,然后清理list,方便内存回收
     */
    private static final int BATCH_COUNT = 300;
    /**
     * 记录导入的总记录数
     */
    private Long listSize = 0L;
    /**
     * 缓存的数据
     */
    private List<ImportItem> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    //标记处理第一行表头
    private boolean firstRowProcessed = true;

    private HashMap<Integer, HashMap<String,String>> dynamicInfoList;

    /**
     * 解析每一行表头数据时调用
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        // 读取表头数据并构造HashMap
        if (firstRowProcessed){
            HashMap<Integer, HashMap<String, String>> infoList = new HashMap<>();
            for (Map.Entry<Integer, String> entry : headMap.entrySet()){
                Integer key = entry.getKey();
                String value = entry.getValue();
                if (!Objects.equals(value, "固定数据")){
                    infoList.put(key,null);
                }
            }
            dynamicInfoList = infoList;
            firstRowProcessed = false;
        }else {
            for (Map.Entry<Integer, String> entry : headMap.entrySet()){
                HashMap<String, String> info = new HashMap<>();
                Integer key = entry.getKey();
                String value = entry.getValue();
                if (dynamicInfoList.containsKey(key)){
                    info.put(value,null);
                    dynamicInfoList.replace(key,info);
                }
            }
        }
    }

    /**
     * 每解析一条数据都会调用一次
     */
    @Override
    public void invoke(ImportItem importItem, AnalysisContext analysisContext) {
        // 获取实体类中不匹配的数据
        ReadRowHolder readRowHolder = analysisContext.readRowHolder();
        Map<Integer, Cell> cellMap = readRowHolder.getCellMap();
        JSONObject dynamicInformation = new JSONObject();
        for (Map.Entry<Integer, Cell> entry : cellMap.entrySet()){
            Integer key = entry.getKey();
            Cell entryValue = entry.getValue();
            if (dynamicInfoList.containsKey(key)){
                String string = JSON.toJSONString(entryValue);
                JSONObject jsonObject = JSON.parseObject(string);
                String value = jsonObject.getString("stringValue");
                HashMap<String, String> info = dynamicInfoList.get(key);
                for (String attribute : info.keySet()) {
                    dynamicInformation.put(attribute,value);
                }
            }
        }
        cachedDataList.add(importItem);
        // 达到BATCH_COUNT了,需要去存储一次数据库,防止数据几万条数据在内存,容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    /**
     * 所有数据解析完成后调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (!cachedDataList.isEmpty()){
            saveData();
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    public void saveData(){
        //将数据存入数据库
    }


}

数据导出

参考文章:EasyExcel动态复杂表头导出方法

相关推荐
会编程的吕洞宾8 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤8 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha8 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
RobinXYuan9 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读
炸薯条!9 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
SimonKing10 小时前
又一款数据库同步神器,2.3K Star的dbswitch
java·后端·程序员
CoderYanger10 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
Rain的Java大神之路11 小时前
前端转 Java 全栈:用 AI 两天交付一个能跑的 Demo,踩坑全记录
java·前端·后端
蜡笔小谢21 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
唐老板1 天前
Spring AI 2.0 只支持 Boot 4
java