SoFunction
Updated on 2025-03-04

Export excel with select checkbox using EasyExcel

1. What is EasyExcel

EasyExcel is a lightweight Excel processing tool that supports file formats in Excel 2003 (xls) and Excel 2007 and above (xlsx). Its main features include:

  • high performance: Parsing Excel files through SAX mode to avoid loading the entire file into memory, suitable for processing large files.
  • Simple and easy to use: Provides a clean and clean API, easy to integrate and use.
  • Rich features: Supports various functions such as custom formats, styles, and formulas.

Principle

The core principle of EasyExcel is to use the SAX mode of Apache POI for parsing. Traditional DOM mode loads the entire Excel file into memory, which can cause memory overflow when processing large files. The SAX mode is event-driven, which only loads data when needed, greatly reducing memory usage.

In terms of writing, EasyExcel avoids loading all data into memory at one time by writing batches, thereby improving writing efficiency.

3. Environmental preparation

Before using EasyExcel, you need to make sure that relevant dependencies have been introduced into your project. You can add EasyExcel dependencies through Maven or Gradle.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId></groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>easyexcel</artifactId>

    <properties>
        <>8</>
        <>8</>
    </properties>
    <dependencies>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.3.4</version>
        </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>lombok</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

4. Basic usage

Data preparation

Before doing Excel operations, you need to prepare your data. The following code shows how to initialize a 10 itemsDemoDataList of data:

List&lt;DemoData&gt; list = ();
for (int i = 0; i &lt; 10; i++) {
    DemoData data = new DemoData();
    ("String" + i);
    (new Date());
    (0.56);
    (data);
}

Write to Excel

EasyExcel provides a simple API to write data to Excel files. Here is a basic write operation example:

String fileName = "";
(fileName, ).sheet("template").doWrite(list);

Advanced Features

Hyperlinks, notes, formulas and styles

EasyExcel supports adding hyperlinks, notes, formulas and styles in cells. The following code shows how to implement these functions:

WriteCellDemoData writeCellDemoData = new WriteCellDemoData();

// Set up hyperlinkWriteCellData&lt;String&gt; hyperlink = new WriteCellData&lt;&gt;("Official Website");
HyperlinkData hyperlinkData = new HyperlinkData();
(hyperlinkData);
("/alibaba/easyexcel");
();

// Setting NotesWriteCellData&lt;String&gt; comment = new WriteCellData&lt;&gt;("Remarked cell information");
CommentData commentData = new CommentData();
(commentData);
("Jiaju Zhuang");
(new RichTextStringData("This is a note"));

// Set the formulaWriteCellData&lt;String&gt; formula = new WriteCellData&lt;&gt;();
FormulaData formulaData = new FormulaData();
(formulaData);
("REPLACE(123456789,1,1,2)");

// Set cell styleWriteCellData&lt;String&gt; writeCellStyle = new WriteCellData&lt;&gt;("Cell Style");
WriteCellStyle writeCellStyleData = new WriteCellStyle();
(FillPatternType.SOLID_FOREGROUND);
(());
(writeCellStyleData);

Custom interceptor

EasyExcel allows users to customize interceptors for more complex functionality. For example, you can add a drop-down box to a cell:

(fileName, )
    .registerWriteHandler(new CustomSheetWriteHandler(()))
    .registerWriteHandler(new CustomCellWriteHandler())
    .sheet("template").doWrite(list);

Insert annotation

Inserting annotations in Excel is also a major feature of EasyExcel. The following code shows how to implement this function:

(fileName, )
    .inMemory()
    .registerWriteHandler(new CommentWriteHandler())
    .sheet("template").doWrite(list);

The above are just some key codes. Please refer to the code repository below.

Code Repository

/Harries/springboot-demo(easypost)

Summarize

EasyExcel is a powerful and easy-to-use Excel processing library suitable for reading and writing Excel files in various scenarios. Through the introduction of this article, I believe you have a preliminary understanding of the basic usage of EasyExcel and some advanced features.

This is the end of this article about using EasyExcel to export excel with selection checkbox. For more related content related to EasyExcel exporting excel, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!