当前位置: 首页 > news >正文

easyexcel与vue配合下载excel

后端

设置响应

// 设置响应头
response.setContentType("application/octet-stream;charset=UTF-8");
String returnName = null;
try {returnName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);
}
response.setHeader("Content-Disposition", "attachment;filename=" + returnName);

往response 写入数据

  • 使用easyexcel自带的方法就行
// 这里 指定文件
try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();) {
// try (ExcelWriter excelWriter = EasyExcel.write(fileName).build()) {if (hasXL) {WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "小料费用差异对比").head(DepreciationEquipXLDto.class).build();// 获取小料费用差异List<DepreciationEquipXLDto> data2 = assetDepreciationSingleDetailService.getDepreciationEquXLCompare(entityList);excelWriter.write(data2, writeSheet2);}if (hasJL) {WriteSheet writeSheet = EasyExcel.writerSheet(0, "其他费用差异对比").head(DepreciationOtherDto.class).build();// 获取其他费用差异List<DepreciationOtherDto> data = assetDepreciationSingleDetailService.getDepreciationOtherCompare(entityList);excelWriter.write(data, writeSheet);WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "胶料费用差异对比").head(DepreciationEquipJLDto.class).build();// 获取胶料费用差异List<DepreciationEquipJLDto> data1 = assetDepreciationSingleDetailService.getDepreciationEquJLCompare(entityList);excelWriter.write(data1, writeSheet1);}WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "总费用差异对比").head(DepreciationTotalDto.class).build();// 获取总费用差异List<DepreciationTotalDto> data3 = assetDepreciationSingleDetailService.getDepreciationTotalCompare(entityList);excelWriter.write(data3, writeSheet3);//     获取具体的折旧明细WriteSheet writeSheet4 = EasyExcel.writerSheet(4, "折旧明细").head(DepreciationSingleDetailDto.class).build();// 获取具体的折旧明细List<DepreciationSingleDetailDto> data4 = assetDepreciationSingleDetailService.getDepreciationSingleDetail(entityList);excelWriter.write(data4, writeSheet4);} catch (IOException e) {throw new RuntimeException(e);
}

前端

  • 后端返回了之后,有响应数据,但是不会自动下载
  • 前端定义的返回类型必须是blob类型
  • 创建隐式a标签,自动点击下载

export function downloadCompareFile(data) {return request({url: '/client/depreciationDetail/downloadCompareFile',method: 'post',data: data,responseType: 'blob' // 表明返回服务器返回})
}
downloadCompareFile(){downloadCompareFile(this.compareDataListParams).then((res) => { // 处理返回的文件流const content = resconst blob = new Blob([content])console.log(content)const fileName = 'xxx.xlsx'if ('download' in document.createElement('a')) { // 非IE下载const elink = document.createElement('a')elink.download = fileNameelink.style.display = 'none'elink.href = URL.createObjectURL(blob)document.body.appendChild(elink)elink.click()URL.revokeObjectURL(elink.href) // 释放URL 对象document.body.removeChild(elink)}})
},

参考

https://blog.csdn.net/weixin_43597991/article/details/127861599

http://www.lryc.cn/news/326168.html

相关文章:

  • Vue.js 模板语法
  • 信号处理--基于DEAP数据集的情绪分类的典型深度学习模型构建
  • Spring设计模式-实战篇之模板方法模式
  • PTA天梯赛习题 L2-006 树的遍历
  • js相关的dom方法
  • Django——Ajax请求
  • 基于java多角色学生管理系统论文
  • python(django)之单一接口管理功能后台开发
  • 教程1_图像视频入门
  • MQTT.fx和MQTTX 链接ONENET物联网提示账户或者密码错误
  • Svn添加用户、添加用户组、配置项目权限等自动化配置脚本
  • Spring事务-两种开启事务管理的方式:基于注解的声明式事务管理、基于编程式的事务管理
  • OC 技术 苹果内购
  • 云原生周刊:Kubernetes v1.30 一瞥 | 2024.3.25
  • 2016年认证杯SPSSPRO杯数学建模D题(第一阶段)NBA是否有必要设立四分线解题全过程文档及程序
  • EdgeGallery开发指南
  • ubuntu arm qt 读取execl xls表格数据
  • STM32 使用gcc编译介绍
  • FPGA之组合逻辑与时序逻辑
  • git clone没有权限的解决方法
  • Redis 的内存回收策略
  • 小程序富文本图片宽度自适应
  • 安装redis时候修改过的配置文件
  • Stable Diffusion 本地部署教程
  • sql如何增加数据
  • 智慧交通(代码实现案例)
  • LeetCode 面试经典150题 205.同构字符串
  • 存内计算:释放潜能的黑科技
  • CentOS Stream 8系统配置阿里云YUM源
  • MySQL Explain 优化参数详细介绍