Vue + axios + SpringBoot 2实现导出Excel
Vue + axios + SpringBoot 2实现导出Excel
1. 前端js代码-发送Http请求
/**
* 文件下载
* @param url 下载地址
* @param fileName 文件名称
* @param params 参数
*/
downloadFile: function (url, params) {
params = params || {}
let httpService = axios.create({
timeout: 300000, // 请求超时时间
headers: {
'Cache-Control': 'no-cache'
}
})
return httpService({
method: 'POST',
url: url,
data: params,
responseType: 'blob'
}).then(res => {
return res.data
})
},
/**
*文件上传
* @param url 上传地址
* @param file 文件对象 target.files <input type='file'> 的文件对象
* @param params 参数可以添加fileName ,type等等
* @returns {Promise<AxiosResponse | never>}
*/
uploadFile: function (url, file, params) {
const formData = new FormData()
params = params || {}
Object.keys(params).map(key => {
formData.append(key, params[key])
})
formData.append('type', params['type'] || 'ReadExcel')
formData.append(params['fileName'] || 'file', file)
let httpService = axios.create({
timeout: 300000, // 请求超时时间
headers: {
'Cache-Control': 'no-cache',
'Content-Type': 'multipart/form-data'
}
})
return httpService.post( url, formData).then(res => {
return res.data
})
}
2. 前端js代码-处理后端返回的流数据(通用处理二进制文件的方法,而不仅仅针对Excel)
/**
@resData 后端响应的流数据
@fileName 文件名称 如果后端设置的是xls/xlsx与后端文件后缀一致。
**/
function dealDownLoadData(resData,fileName){
try { let blob ;
if(resData instanceof Blob){
blob = resData;
}else{
blob = new Blob([resData], { type: resData.type});
}
if (!!window.ActiveXObject || "ActiveXObject" in window) { //IE浏览器
//navigator.msSaveBlob(blob, fileName); //只有保存按钮
navigator.msSaveOrOpenBlob(blob, fileName); //有保存和打开按钮
}else{
var linkElement = document.createElement('a');
var url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", fileName);
var clickEvent = new MouseEvent("click",
{
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
}
} catch (ex) {
console.log(ex);
} }
3.导出Excel
/**
* 导出Excel
* @param request
* @return
* @throws Exception
*/
@RequestMapping(value = "/xxx")
public ResponseEntity<Resource> downloadFileApi() throws Exception {
//Excel场景一:直接创建,然后编辑内容
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); //Excel场景二:读取模板,然后在模板中编辑内容
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream("/template.xls"));
hssfWorkbook = new HSSFWorkbook(poifsFileSystem); //写到输出流中
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
hssfWorkbook.write(outputStream);
//文件名称:注意:这里的后缀名称必须是xls或 xlsx,不然不能识别为excel
String fileName = "xxx.xls";
//返回
ByteArrayInputStream is = new ByteArrayInputStream(outputStream.toByteArray());
//调用通用下载文件方法
return this.downloadFile(is, fileName); }
/**
* 通用的下载方法,可以下载任何类型文件
* @param is
* @param fileName
* @return
* @throws IOException
*/
public ResponseEntity<Resource> downloadFile(InputStream is,String fileName) throws IOException{ HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("charset", "utf-8");
//设置下载文件名
headers.add("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
Resource resource = new InputStreamResource(is);
return ResponseEntity.ok()
.headers(headers)
//根据文件名称确定文件类型。
.contentType(MediaType.parseMediaType(HttpKit.getMimeType(fileName)))
.body(resource);
}
Vue + axios + SpringBoot 2实现导出Excel的更多相关文章
- SpringBoot使用Easypoi导出excel示例
SpringBoot使用Easypoi导出excel示例 https://blog.csdn.net/justry_deng/article/details/84842111
- Vue框架下实现导入导出Excel、导出PDF
项目需求:开发一套基于Vue框架的工程档案管理系统,用于工程项目资料的填写.编辑和归档,经调研需支持如下功能: Excel报表的导入.导出 PDF文件的导出 打印表格 经过技术选型,项目组一致决定通过 ...
- VUE中使用XLSX实现导出excel表格
简介 项目中经常会用导出数据的场景,这里介绍 VUE 中如何使用插件 xlsx 导出数据 安装 ## 1.使用 npm 或 yarn 安装依赖(三个依赖) npm install -S file-sa ...
- Vue通过Blob对象实现导出Excel功能
不同的项目有不同的导出需求,有些只导出当前所显示结果页面的表格进入excel,这个时候就有很多插件,比如vue-json-excel或者是Blob.js+Export2Excel.js来实现导出Exc ...
- springboot通过poi导出excel
Maven引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...
- Vue项目中将table组件导出Excel表格以及打印页面内容
体验更优排版请移步原文:http://blog.kwin.wang/programming/vue-table-export-excel-and-print.html 页面中显示的table表格,经常 ...
- # vue 如何通过前端来导出excel表格
在做一些简单的demo时,偶尔会遇到导出excel表格.如果请后端帮忙的话 比较浪费时间,那么前端如何导出excel表格,下面就来记录一下之前使用到的案例 一.安装依赖 npm i file-save ...
- SpringBoot之导入导出Excel
1.添加springBoot支持 <dependency> <groupId>org.apache.poi</groupId> <artifactId> ...
- vue axios springBoot 跨域session丢失
前端: 在引入axios的地方配置 axios.defaults.withCredentials=true,就可以允许跨域携带cookie信息了,这样每次发送ajax请求后,只要不关闭浏览器,得到的s ...
随机推荐
- 没有学历如何从事Java开发?
学历成了当今社会一个衡量一个人能力的标准,未来只会越来越深入,也有的人说不要总是把学历挂嘴边,学历并不能代表能力,确实学历不能代表能力,但是学历是能代表一个的人学习深度,也是在职场上必备的一个敲门砖. ...
- PHP acos() 函数
实例 返回不同数的反余弦: <?phpecho(acos(0.64) . "<br>");echo(acos(-0.4) . "<br>&q ...
- PHP asXML() 函数
实例 格式化 XML(版本 1.0)中的 SimpleXML 对象的数据: <?php$note=<<<XML<note>高佣联盟 www.cgewang.com& ...
- 5.5 省选模拟赛 B Permutation 构造 贪心
LINK:Permutation 对于这种构造神题 我自然是要补的.为啥就我没想出来哇. 30分还是很好写的 注意8!实际上很小 不需要爆搜 写bfs记录状态即可.至于判断状态是否出现与否 可以开ma ...
- scala下划线的用法
1.作为“通配符”,类似Java中的*.如import scala.math._2.:_*作为一个整体,告诉编译器你希望将某个参数当作参数序列处理!例如val s = sum(1 to 5:_*)就是 ...
- linux集群服务网络状态(netstat),服务端页面(图形字符页面)基本配置
Linux网络基础配置 yum -y install vim 安装vim 关闭的防火墙服务 iptables -F iptables -X iptables -Z systemctl s ...
- Windows-快速预览文件-QuickLook
开源.免费的文件快速预览工具, 支持图片.文档.音视频.代码文本.压缩包等多种格式. 获得 Mac OS 空格键快速预览文件相同的体验 效果图 文件夹 音视频 浏览 压缩包,文本 支持的格式: 图片: ...
- 华为云的研究成果又双叒叕被MICCAI收录了!
摘要:2020年国际医学图像计算和计算机辅助干预会议(MICCAI 2020),论文接收结果已经公布:华为云医疗AI团队和华中科技大学合作的2篇研究成果入选. 语义/实例分割问题是近年来医学图像计算领 ...
- PhpStorm配置Apache与php的运行环境详细教程
本文主要说明如何在phpstorm中配置已经安装好的PHP与apache.首先需要在本地安装php,这里我安装的是phpstudy 进入PHPstorm的界面点击file 下的settings 在La ...
- 使用cors完成跨域请求处理
跨域的含义 同源策略以及其限制内容 同源策略是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,浏览器很容易受到XSS.CSFR等攻击.所谓同源是指"协议+域名+端口&quo ...