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.
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.
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools