我是靠谱客的博主 靓丽鼠标,这篇文章主要介绍JAVA使用EasyExcel 进行文件的下载Spring Boot中使用EasyExcel 进行文件的下载1.引入依赖2.创建实体类3. 控制器中的方法3.在业务层获取数据集合(Service类)4.前端Vue 方法代码,现在分享给大家,希望可以做个参考。

Spring Boot中使用EasyExcel 进行文件的下载

1.引入依赖

复制代码
1
2
3
4
5
6
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.1.1</version> </dependency>

2.创建实体类

复制代码
1
2
3
4
5
6
7
8
9
10
11
@Data @EqualsAndHashCode(callSuper = false) public class User { @ExcelProperty("id") private String id; @ExcelProperty("姓名") private String name; @ExcelProperty("密码") private String password; }

3. 控制器中的方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Autowired private Service service; @PostMapping("/export") public void download(HttpServletResponse response) throws IOException { // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); //user.class 为实体类,date()是返回的集合 EasyExcel.write(response.getOutputStream(), User.class).sheet("模板").doWrite(service.data()); }

3.在业务层获取数据集合(Service类)

复制代码
1
2
3
4
5
6
7
8
9
private List<User> data() { List<User> users = new ArrayList<>(); User user = new User(); user.setName("张三"); user.setPassword("123456"); users.add(user); return users; }

4.前端Vue 方法代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const updown = () => { axios({ // 用axios发送get请求 method: 'post', url: 'http://localhost:xxx', // 请求地址 responseType: 'blob', // 表明返回服务器返回的数据类型 // data:patient.patient, //提交的数据 headers: { 'Content-Type': 'application/json' } }).then(res => { // 处理返回的文件流 //new Blob([res])中不加data就会返回下图中[objece objece]内容(少取一层) const blob = new Blob([res.data],{type: "application/vnd.ms-excel"}); const fileName = '文件名称.xlsx';//下载文件名称 const elink = document.createElement('a'); elink.download = fileName; elink.style.display = 'none'; elink.href = URL.createObjectURL(blob); document.body.appendChild(elink); elink.click(); URL.revokeObjectURL(elink.href); // 释放URL 对象 document.body.removeChild(elink); }) };

最后

以上就是靓丽鼠标最近收集整理的关于JAVA使用EasyExcel 进行文件的下载Spring Boot中使用EasyExcel 进行文件的下载1.引入依赖2.创建实体类3. 控制器中的方法3.在业务层获取数据集合(Service类)4.前端Vue 方法代码的全部内容,更多相关JAVA使用EasyExcel内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部