SpringBoot中实现一个通用Excel导出功能

这个导出功能的特色

  • 非常通用,能够满足大部分项目中99%的导出功能,代码复用性很强
  • 导出的列可以由前端自定义(比如哪些列需要导出、列的顺序、名称等都是可以由前端自定义)

看效果

先带大家看一下效果。

在这里插入图片描述

代码解析

1、依赖

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-boot.version>2.7.13</spring-boot.version>
        <commons.io.version>2.11.0</commons.io.version>
        <mybatis-plus.version>3.5.3</mybatis-plus.version>
        <hutool.version>5.8.2</hutool.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- io常用工具类 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons.io.version}</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
        </dependency>

        <!-- Apache Lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <!-- Apache collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.4</version>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <image>
                        <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                    </image>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

2、Excel 入参(ExcelExportRequest)

public class ExcelExportRequest {
   
    /**
     * excel名称
     */
    private String excelName;

    /**
     * sheet的名称
     */
    private String sheetName;

    /**
     * 导出字段有序列表
     */
    private List<ExcelExportField> fieldList;

    public String getSheetName() {
   
        return sheetName;
    }

    public void setSheetName(String sheetName) {
   
        this.sheetName = sheetName;
    }

    public String getExcelName() {
   
        return excelName;
    }

    public void setExcelName(String excelName) {
   
        this.excelName = excelName;
    }

    public List<ExcelExportField> getFieldList() {
   
        return fieldList;
    }

    public void setFieldList(List<ExcelExportField> fieldList) {
   
        this.fieldList = fieldList;
    }
}

3、Excel 出参(ExcelExportResponse)

public class ExcelExportResponse {
   
    //导出的excel文件名称
    private String excelName;
    // sheet列表数据
    private List<ExcelSheet> sheetList;

    public String getExcelName() {
   
        return excelName;
    }

    public void setExcelName(String excelName) {
   
        this.excelName = excelName;
    }

    public List<ExcelSheet> getSheetList() {
   
        return sheetList;
    }

    public void setSheetList(List

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部