阿里巴巴easyexcel导出文件
时间: 2025-06-01 14:09:44 浏览: 19
### 使用阿里巴巴 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 接口以及前端调用,可以轻松实现这一需求。
---
###
阅读全文
相关推荐


















