easyexcel 批量导出
时间: 2023-06-29 18:02:42 浏览: 122
Excel批量导出
使用EasyExcel批量导出数据非常方便,以下是一个示例:
1. 引入EasyExcel依赖
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.11</version>
</dependency>
```
2. 创建导出的数据实体类
```java
@Data
public class ExportData {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
@ExcelProperty("性别")
private String gender;
}
```
3. 编写导出方法
```java
public void export(List<ExportData> dataList, HttpServletResponse response) throws IOException {
// 设置响应头
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("导出数据.xlsx", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
ServletOutputStream outputStream = response.getOutputStream();
// 使用EasyExcel进行导出
ExcelWriter excelWriter = EasyExcel.write(outputStream, ExportData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet("导出数据").build();
excelWriter.write(dataList, writeSheet);
excelWriter.finish();
}
```
4. 调用导出方法
```java
List<ExportData> dataList = new ArrayList<>();
// 添加数据到dataList中
export(dataList, response);
```
以上就是使用EasyExcel批量导出数据的简单示例。
阅读全文