easyexcel导出
时间: 2023-07-30 07:07:15 浏览: 52
EasyExcel是一个基于Java的Excel读写解决方案,可以方便地进行Excel导入和导出操作。下面是一个简单的EasyExcel导出示例:
首先,需要在项目中添加EasyExcel的依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version>
</dependency>
```
然后,创建一个Java对象,用于存储需要导出的数据,例如:
```java
@Data
public class User {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
@ExcelProperty("性别")
private String sex;
}
```
接下来,创建一个Controller方法,用于处理导出请求:
```java
@GetMapping("/export")
public void export(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);
List<User> userList = userService.getUserList();
try (OutputStream outputStream = response.getOutputStream()) {
EasyExcel.write(outputStream, User.class).sheet("用户信息").doWrite(userList);
}
}
```
在上面的代码中,我们首先设置响应类型和编码,并设置文件名。然后获取需要导出的数据列表,使用EasyExcel的`write`方法,将数据写入输出流中。
最后,我们可以在前端页面添加一个下载按钮,点击该按钮即可下载导出的Excel文件。
这是一个简单的EasyExcel导出示例,你可以根据自己的需求进行调整和扩展。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)