alibaba easyexcel 导出excel使用

需求

传统导出,一般都是通过Workbook > Sheet > Row > Cell 获取详细Cell 设置值,比较麻烦,偶然遇到alibaba easyexcel 直接通过注解设置哪些需要导出 哪些忽略,发现特别好用。

pom依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.1</version>
        </dependency>

完整pom.xml

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

    <groupId>org.example</groupId>
    <artifactId>tool-box-export</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.demo</groupId>
            <artifactId>tool-box-api</artifactId>
            <version>1.0-SNAPSHOT</ver
### 使用阿里巴巴 EasyExcel 导出 Excel 文件的示例代码 以下是使用阿里巴巴 EasyExcel导出 Excel 文件的具体方法和示例代码。此方法结合了后端服务层、控制器层以及前端调用的完整流程。 #### 1. 实体类定义 首先需要定义一个实体类,该类将作为 Excel 表格的数据模型,并通过注解 `@ExcelProperty` 指定表头信息和列索引。 ```java public class StudentInfo extends BaseRowModel { /** 学生ID */ @ExcelProperty(value = "学生ID", index = 0) private Long studentId; /** 姓名 */ @ExcelProperty(value = "姓名", index = 1) private String name; /** 年龄 */ @ExcelProperty(value = "年龄", index = 2) private Integer age; /** 性别 */ @ExcelProperty(value = "性别", index = 3) private String gender; // Getter 和 Setter 方法 public Long getStudentId() { return studentId; } public void setStudentId(Long studentId) { this.studentId = studentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } } ``` #### 2. Service 层实现 在服务层中实现具体的导出逻辑,使用 EasyExcel 提供的 `ExcelWriter` 类完成数据写入操作。 ```java import com.alibaba.excel.EasyExcel; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.List; public class EasyexcelService { public void exportStudentInfoExcel(HttpServletResponse response) throws IOException { // 设置响应头信息 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("学生信息表", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); // 准备要导出的数据 List<StudentInfo> data = getData(); // 写入 Excel 文件 EasyExcel.write(response.getOutputStream(), StudentInfo.class).sheet("学生信息").doWrite(data); } private List<StudentInfo> getData() { // 模拟生成一些测试数据 List<StudentInfo> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { StudentInfo studentInfo = new StudentInfo(); studentInfo.setStudentId((long) (i + 1)); studentInfo.setName("学生" + (i + 1)); studentInfo.setAge(18 + i); studentInfo.setGender(i % 2 == 0 ? "男" : "女"); list.add(studentInfo); } return list; } } ``` #### 3. Controller 层实现 在控制器层中提供一个 HTTP 接口供前端调用。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @RestController public class EasyexcelController { @Autowired private EasyexcelService easyexcelService; @GetMapping(value = "/export") public void exportStudentInfoExcel(HttpServletResponse response) throws IOException { easyexcelService.exportStudentInfoExcel(response); } } ``` #### 4. 前端调用 前端可以通过点击按钮触发导出操作,以下是一个基于 Layui 的前端示例。 ```javascript function exports() { layer.confirm('确定导出所有策略的信息?', { btn: ['确定', '取消'] }, function(index) { // 确认导出 location.href = '/export'; layer.close(index); }, function(index) { // 取消导出 layer.close(index); }); } ``` 以上代码展示了如何使用阿里巴巴 EasyExcel 库完成 Excel 文件的导出功能[^1]。通过定义实体类、编写服务层逻辑、暴露 RESTful 接口以及前端调用,可以轻松实现这一需求。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心系代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值