当前位置:网站首页>Easyexcel configuration and Application

Easyexcel configuration and Application

2022-06-11 13:37:00 QLUGCL

Excel Import and export application scenarios

1 、 Data import : Reduce the input workload
2 、 Export data : Statistical information archiving
3 、 The data transfer : Data transmission between heterogeneous systems

EasyExcel characteristic

  • Java Domain analysis 、 Generate Excel Well known frameworks are Apache poi、jxl etc. . But they all have a serious problem that is very memory consuming . If your system has a small amount of concurrency, it may be OK , But once concurrency comes up, it will OOM perhaps JVM Frequent full gc.
  • EasyExcel Alibaba is an open source excel Processing framework , To use the Simple 、 Save memory Is famous for its .EasyExcel The main reason that can greatly reduce the memory occupation is parsing Excel The file data is not loaded into memory at one time , Instead, read data from a row on the disk , One by one .
  • EasyExcel Line by line parsing mode ( read ), And the parsing results of one line are notified and processed in the observer mode (AnalysisEventListener)

EayExcel The bottom layer of the is to interact directly with the disk , And read the table row by row , Memory usage is very small , The test results are shown below .

EasyExcel To configure

pom Introduction in xml Related dependencies

<dependencies>
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>

contrast excel Column attribute create entity class

Set the header and added data fields get,set Method

@Data
public class DemoData {
    
    // Set up excel Header name ,index The first few columns 
    @ExcelProperty(value = " Student number ",index = 0)
    private Integer sno;
    // Set header name 
    @ExcelProperty(value = " The student's name ",index = 1)
    private String sname;
}

Implement write operation ( Write data to excel)

 Please add a picture description

        // Realization excel The operation of writing 
        //1  Set the write folder address and excel File name 
        String filename = "F:\\write.xlsx";
        //2  call easyexcel The method inside realizes the write operation 
        //write Method two parameters : First parameter file path name , Second parameter entity class .class= The path of the entity class ,sheet yes excel The subscript of the file , The file stream closes automatically 
        EasyExcel.write(filename,DemoData.class).sheet(" Student list ").doWrite(getData());

Implement read operation ( read out excel data )

  • read excel operation , Mainly in the set listening class invoke Processing entity class parameters in the function , Realize the functions of reading data and processing data ( Use the incoming service Parameters are processed
  • Read excel Content , Read line by line excel Content , Do not read the header , And then use invoke Function implements line by line processing through entity class parameters ( if excel There are multiple lines of data ,invoke Will be called many times , Pass entity class parameters multiple times ( Store each line ))
  • Because reading excel Monitor class AnalysisEventListener< Entity class > Can't give it to spring Conduct management ( Cannot use annotations ), Need yourself new, Cannot inject other objects , Unable to implement database operation , Because you can't inject service,mapper wait , Of course, you can also directly use the most primitive jdbc Way
  • When instantiating the listening class, you will service Pass as a constructor parameter
    new SubjectExcelListener(subjectService))
     Please add a picture description

Operation code

  try {
    
            // File input stream 
            InputStream in = file.getInputStream();
            // Call the method to read the incoming excel and service object 
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch(Exception e){
    
            e.printStackTrace();
        }

Monitor class

package com.qlugcl.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qlugcl.eduservice.entity.EduSubject;
import com.qlugcl.eduservice.entity.excel.SubjectData;
import com.qlugcl.eduservice.service.EduSubjectService;
import com.qlugcl.servicebase.exceptionhandler.GCLException;

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
    

    // because SubjectExcelListener Can't give it to spring Conduct management ( Cannot use annotations ), Need yourself new, Cannot inject other objects 
    // Unable to implement database operation , Because you can't inject service,mapper wait , Structural method with parameters , Of course, you can also directly use the most primitive jdbc The way to 
    public EduSubjectService subjectService;
    public SubjectExcelListener() {
    }
//  Here by passing service Parameters , So as to realize the operation of the database 
    public SubjectExcelListener(EduSubjectService subjectService) {
    
        this.subjectService = subjectService;
    }
    // Read excel Content , Read line by line 
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
    
        if(subjectData == null) {
    
            throw new GCLException(20001," File data is empty ");
        }

        // Read line by line , There are two values per read , The first value is a primary classification , The second value is the secondary classification 
        // Judge whether the classification is repeated , Whether there is 
        EduSubject existOneSubject = this.existOneSubject(subjectService, subjectData.getOneSubjectName());
        if(existOneSubject == null) {
     // There is no same primary classification , Add 
            existOneSubject = new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(subjectData.getOneSubjectName());// First class classification name 
            subjectService.save(existOneSubject);
        }

        // Get the first level classification id value 
        String pid = existOneSubject.getId();

        // Add secondary classification 
        // Judge whether the secondary classification is repeated , Whether there is 
        EduSubject existTwoSubject = this.existTwoSubject(subjectService, subjectData.getTwoSubjectName(), pid);
        if(existTwoSubject == null) {
    
            existTwoSubject = new EduSubject();
            existTwoSubject.setParentId(pid);
            existTwoSubject.setTitle(subjectData.getTwoSubjectName());// Secondary classification name 
            subjectService.save(existTwoSubject);
        }
    }

    // Judge that the primary classification cannot be added repeatedly 
    private EduSubject existOneSubject(EduSubjectService subjectService,String name) {
    
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id","0");
        EduSubject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    // Judge that the secondary classification cannot be added repeatedly 
    private EduSubject existTwoSubject(EduSubjectService subjectService, String name, String pid) {
    
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("title",name);
        wrapper.eq("parent_id",pid);
        EduSubject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    
    }
}


EasyExcel read excel application

 Please add a picture description
read excel operation , Mainly in the set listening class invoke Function to read and process data

package com.createcode.excel;

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

import java.util.Date;
import java.util.Map;

//excel Reading listener , Once a read is generated excel operation , Then the corresponding configuration for reading will be triggered 
public class ExcelListener extends AnalysisEventListener<DemoData> {
    
    // Read line by line excel Content , Do not read the header , stay invoke Function to operate on the read data 
    @Override
    public void invoke(DemoData data, AnalysisContext analysisContext) {
    
        System.out.println("****"+data+""+new Date());
        try {
    
            Thread.sleep(1000);

        } catch (InterruptedException e) {
    
            e.printStackTrace();

        }
    }
    // Read the contents of the header 
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    
        System.out.println(" Header :"+headMap);
    }
    // After the read is complete 
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
     }
}




summary

Read operations : read out excel The data is listening for the class invoke Function to process row by row entity class parameters and store them in the database

As shown in the figure :easyexcel Just handle excel Data interaction with the database , Format converted
 Insert picture description here
 Insert picture description here

 Insert picture description here

原网站

版权声明
本文为[QLUGCL]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012117344503.html