SoFunction
Updated on 2025-05-08

POI can easily complete EasyExcel usage and Excel import and export functions from the beginning to the actual combat

Preface:

Apache POI

is a popular Java library for processing Microsoft Office format files and provides rich APIs to create, read, and modify Office documents.

1.Official websiteApache POI™ - the Java API for Microsoft Documents

2.advantage: Powerful function, can handle complex Excel files; high stability, widely supported and maintained; good flexibility, can meet customization needs.

3.shortcoming: The learning curve is steep and costly for beginners; performance is affected when processing large amounts of data.

EasyPoi

The main functions are simple and easy to use. Through simple annotations and template language, you can realize Excel export, import, Word template export and other functions.

1.Official website/lemur/easypoi

2.advantage: The API interface is simple and convenient to operate; it has rich functions and supports multiple operations; it is based on Apache POI and JexcelApi, easy to expand; the documentation is detailed, easy to learn and use.

3.shortcoming: Compared with Apache POI, advanced features are limited; due to the existence of the encapsulation layer, processing large amounts of data has performance losses.

EasyExcel

It is an open source framework based on Apache POI packaged by Alibaba, focusing on reading and writing Excel files.

1.Official websiteEasyExcel official document - Java-based Excel processing tool | Easy Excel official website

2.advantage: The API is simple and easy to use; high performance when processing large amounts of data; supports annotation configuration import and export rules to simplify development.

3.shortcoming: Compared with Apache POI, the functions are simple and cannot meet complex needs; the customization capabilities are not as flexible as Apache POI.

1. EasyExcel

EasyExcel is a simple, memory-saving read and write Excel tool based on Java by Alibaba. When dealing with large files, it greatly reduces memory usage and is relatively simple to use.

1.1. Core features

Low memory usage:It adopts line-by-line reading and writing methods, without loading the entire Excel file into memory, avoiding memory overflow problems, and is suitable for handling large data volumes of Excel files. For example, when processing Excel files containing tens of thousands of pieces of data, traditional methods can lead to insufficient memory, and EasyExcel can handle it easily.

API is simple:It provides a concise API, and can complete the reading and writing mapping of Excel data through annotations, reducing development costs and learning costs. Developers can map data to Excel cells by simply configuring annotations.

Feature-rich:It supports Excel's import and export, data verification, custom formatting and other functions, and can also handle complex table headers and multi-level table headers.

High performance:Excellent in read and write performance, can quickly process large amounts of data, and improve program execution efficiency.

1.2. Common annotations

@ExcelProperty:It is the most commonly used annotation for specifying the header name, order and other information of an Excel column. You can specify the header name through the value attribute and specify the index of the column through the index attribute.

@DateTimeFormat:Used to format date type data. For example, @DateTimeFormat("yyyy-MM-dd") can format dates as specified strings.

@NumberFormat:Used to format data of numeric types. For example, @NumberFormat("#.##%") can format numbers as a percentage form.

1.3. Use steps

1.3.1. Introducing dependencies

<dependency>
    <groupId></groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.3.2</version>
</dependency>

1.3.2. Create entity objects

@Data
public class SysUserData {
    @ExcelProperty("User Name")
    private String uname;
    @ExcelProperty("Real Name")
    private String iphone;
    @ExcelProperty("username")
    private String realName;
    @ExcelProperty("password")
    private String ipassword;
    @ExcelProperty("Login ip")
    private String lastloginip;
    @ExcelProperty("Login time")
    private String lastlogintime;
    @ExcelProperty("Login times")
    private String logintimes;
    @ExcelProperty("Account Status")
    private int state;
}

1.3.3. Import

/**
      * easyExcl
      * Import excel
      * @return
      */
    @RequestMapping(value = "/easyExcelImport")
    public List&lt;SysUser&gt; easyExcelImport() {
        List&lt;SysUser&gt; uList = null;
        try {
            uList = new ArrayList&lt;&gt;();
            String filePath = "/Users/admin/Desktop/";
            List&lt;SysUserData&gt; userList = (filePath).head().sheet().doReadSync();
            for (SysUserData user : userList) {
                SysUser sysUser = new SysUser();
                (());
                (());
                (());
                (());
                (());
                (((())));
                (());
                (());
                (sysUser);
                (sysUser);
            }
        } catch (Exception e) {
            ();
        }
        return uList;
    }

1.3.4. Export

 /**
      * easyexcel
      * Export
      * @param response
      * @throws IOException
      */
    @RequestMapping(value = "/easyExcelWrite")
    public void easyExcelWrite(HttpServletResponse response) throws IOException {
        ("utf-8");
        long timestamp = () / 1000;
        String fileName = "User Information_" + timestamp + ".xlsx";
        String encodedFileName = (fileName, "UTF-8");
        ("Content-disposition", "attachment;filename*=utf-8''" + encodedFileName);
        List&lt;SysUserData&gt; dataList = new ArrayList&lt;SysUserData&gt;();
        // data        List&lt;SysUser&gt; userList = ();
        for (SysUser user : userList) {
            SysUserData SysUserData = new SysUserData();
            (());
            (());
            (());
            (());
            (());
            ((()));
            (());
            (());
            (SysUserData);
        }
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy((), ());
        try (
        ExcelWriter excelWriter = ((), )
                .registerWriteHandler(new CellWriteWidthConfig()) // Adaptive column width                .registerWriteHandler(new CellWriteHeightConfig()) // Adaptive height                .registerWriteHandler(horizontalCellStyleStrategy)
                .build()) {
            (dataList, ("User Information").build());
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

Through the above content, you can easily use EasyExcel. Is it super easy? If you have any questions, please leave a message!!!

This is the article about POI from getting started to practical use of EasyExcel, and completing Excel import and export. This is the end. For more related POI EasyExcel content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!