SpringBoot中实现一个通用Excel导出功能
文章目录
- SpringBoot中实现一个通用Excel导出功能
-
- 这个导出功能的特色
- 看效果
- 代码解析
-
- 1、依赖
- 2、Excel 入参(ExcelExportRequest)
- 3、Excel 出参(ExcelExportResponse)
- 4、ExcelExportField
- 5、ExcelExportUtils 工具类
- 6、ExcelHead 头部
- 7、ExcelSheet 角标
- 8、Aop拦截请求,将 ExcelExportResponse 处理成excel导出
- 9、页面
- 10、接口
- 10、效果
这个导出功能的特色
- 非常通用,能够满足大部分项目中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