Home >Java >javaTutorial >How to import multi-level headers in Java easyExcel

How to import multi-level headers in Java easyExcel

WBOY
WBOYforward
2023-05-07 13:18:123452browse

First of all, we need to understand

How easyExcel gets the header and directly paste the code without talking nonsense

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;

import java.util.*;

@Data
public class AnalysisEventMonitor extends AnalysisEventListener<Map<Integer, String>> {
    /**
     * 存储Key
     */
    Map<Integer, String> key = new HashMap<>();
    /**
     * keuList
     */
    List<String> keyList=new ArrayList<>();

    public AnalysisEventMonitor() {
    }
    /**
     * 重写invokeHeadMap方法,获去表头,如果有需要获取第一行表头就重写这个方法,不需要则不需要重写
     *
     * @param headMap Excel每行解析的数据为Map<Integer, String>类型,Integer是Excel的列索引,String为Excel的单元格值
     * @param context context能获取一些东西,比如context.readRowHolder().getRowIndex()为Excel的行索引,表头的行索引为0,0之后的都解析成数据
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        Set<Integer> integerSet = headMap.keySet();
        for (Integer integer : integerSet) {
            keyList.add(headMap.get(integer));
        }
        key.putAll(headMap);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {

    }
}

This code is used to intercept the header when easyExcel reads the excel table

Create a class yourself and inherit AnalysisEventListener, and then rewrite her invokeHeadMap method to get the first-level header of excel.

The following code for importing a file

//加载拦截器 作用于 获取表头
            AnalysisEventMonitor analysisEventMonitor = new AnalysisEventMonitor ();
            //读取导入的excel 这个地方容易报错 0✖什么的 这个时候就去把excel的文件用高级版本的excel重新导出 因为版本过低的问题
            List<Map<Integer,Object>> list = EasyExcel.read(file.getInputStream(),analysisEventMonitor ).sheet(0).doReadSync();
            //获取拦截器拦截到的 表头的Map集合
            Map<Integer, String> key = analysisEventMonitor .getKey();

Now we have the data and the table header. Some people will be curious, you only got the first-level header, what about my second-level header? Don't worry. Listen to me slowly.

The code block above, the second line obtains the excel data. This data is the data after removing the first-level header. When it comes to this, some people may understand it, and some people still don’t understand it. OK Let's continue. The third line of code is a collection of table headers intercepted by our interceptor. It can be seen that the key of the map received by map is an Integer type. By coincidence, our data list also contains a map and map The key happens to be of Integer type. At this time, students with good logic must have thought that this integer is not randomly written and sorted, but the position of the header corresponds to the data. Then it’s easy at this time. We only need to find the starting position of the multi-level header. I will post the picture below and then answer it.

How to import multi-level headers in Java easyExcel

See that the results are now merged multi-level headers. At this time, in the header set we obtained, 1 corresponds to the name, 2 corresponds to the phone number, and 3 corresponds to the score. The obtained data list is written in list.get(0); the obtained data is the data in the secondary header. At this time, another map is obtained. The corresponding relationships are 1=null 2=null 3=English 4= language. At this time, someone asked me again: What if I have a multi-level header later? stickers.

How to import multi-level headers in Java easyExcel

In the header data we obtained at this time, 1=name 2=phone number 3=score 4=null 5=in the list data obtained from the assessment results, 1=null 2=null 3=English 4=Chinese 5=English 6=Chinese
This ends the import of multiple headers. Some people still don’t know how to save it into the database, so you have to think about it. The stupidest method is to remember to fill the subscript into the entity class. This method is not recommended and is inflexible! The best way is to use an enumeration to match the text, then match the field, fill in the entity class, or write a method to match the text with the field and automatically convert the entity class.

The above is the detailed content of How to import multi-level headers in Java easyExcel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete