当前位置:网站首页>Easyexcel introduction-01

Easyexcel introduction-01

2022-06-21 07:04:00 Sister min

brief introduction

Java analysis 、 Generate Excel Well known frameworks are Apache poijxl. But they all have a serious problem that is very memory consuming ,poi There is a set SAX Mode API It can solve some memory overflow problems to a certain extent , but POI There are still some flaws , such as 07 edition Excel Decompression and storage after decompression are done in memory , Memory consumption is still high .easyexcel Rewrote poi Yes 07 edition Excel Parsing , One 3M Of excel use POI sax Parsing still needs 100M Left and right memory , change to the use of sth. easyexcel It can be reduced to a few M, And the bigger excel There will be no memory overflow ;03 Version depends on POI Of sax Pattern , In the upper layer, the encapsulation of model transformation is done , Make the user more simple and convenient

Project structures,

Import dependence

<!--easyExcel Dependency import -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.3</version>
</dependency>

Write test entity classes

package entry;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/** * Created by jdx on 2022/6/17  In the morning 12:10 */
/* Equivalent to... In entity class get set Method */
@Data
/* Equivalent to a parametric structure */
@AllArgsConstructor
/* Equivalent to nonparametric construction */
@NoArgsConstructor
public class Student {
    
    /*@ExcelProperty * 1、 Details are equivalent to the header definition  * 2、 Entity alias  * */
    @ExcelProperty(" full name ")
    private String name;

    @ExcelProperty(" Admission time ")
    private String admissionATime;

    @ExcelProperty(" Gender ")
    private Boolean sex;

    @ExcelProperty(" Contact information ")
    private String contactDetails;

    @ExcelProperty(" class ")
    private String className;

    @ExcelProperty(" achievement ")
    private String score;

    @ExcelProperty(" mailbox ")
    private String e_mail;
}

Writing test classes

package test;

import com.alibaba.excel.EasyExcel;
import org.junit.Test;
import entry.Student;

import java.util.ArrayList;

/** * Created by jdx on 2022/6/17  In the morning 12:13 */
public class test {
    
    @Test
    public static void main(String[] args) {
    
        /* Create a new... To store the output stream object list*/
        ArrayList<Student> students = new ArrayList<Student>();

        /* Create a new one Student object */
        Student student01 = new Student(" Miyamoto musashi ","2016-03",true,"12345678911"," Class 11, grade 9 ","A","[email protected]");
        Student student02 = new Student(" Nacoro ","2016-03",false,"12345678912"," Class 11, grade 9 ","A","[email protected]");
        Student student03 = new Student(" I don't know sun ","2016-03",false,"12345678913"," Class 11, grade 9 ","A","[email protected]");
        Student student04 = new Student(" Orange right Beijing ","2016-03",true,"12345678914"," Class 11, grade 9 ","A","[email protected]");
        Student student05 = new Student(" The Monkey King ","2016-03",true,"12345678915"," Class 11, grade 9 ","A","[email protected]");
        Student student06 = new Student(" Master Sanzang ","2016-03",true,"12345678916"," Class 11, grade 9 ","A","[email protected]");
        Student student07 = new Student(" Pig eight quit ","2016-03",true,"12345678917"," Class 11, grade 9 ","A","[email protected]");
        Student student08 = new Student(" Yao ","2016-03",true,"12345678918"," Class 11, grade 9 ","A","[email protected]");
        Student student09 = new Student(" Li Xiaoyao ","2016-03",true,"12345678919"," Class 11, grade 9 ","A","[email protected]");
        Student student10 = new Student(" Zhaoliner ","2016-03",false,"12345678910"," Class 11, grade 9 ","A","[email protected]");
        Student student11 = new Student(" Li Bai a","2016-03",true,"12345678922"," Class 11, grade 9 ","A","[email protected]");

        /* Add data */
        students.add(student01);
        students.add(student02);
        students.add(student03);
        students.add(student04);
        students.add(student05);
        students.add(student06);
        students.add(student07);
        students.add(student08);
        students.add(student09);
        students.add(student10);
        students.add(student11);

        /* Define file name */
        String fileName = " On the school roster .xlsx";

        /* Test output mode  * 1、.write  Method parameter  *  File name  *  Construction object  * 2、.sheet  Method parameter  * sheet name  * 3、.doWrite  Method  *  Incoming and output stream objects */
        EasyExcel.write(fileName,Student.class).sheet("ceshi").doWrite(students);

    }

}

test result

 Insert picture description here

So far, the project has been successfully built and simple tests have been carried out .

原网站

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