vue 把后台返回的json拼接成excel并下载
先封装一下生成excel的方法
downfile.js
export default {
data() {
return {}
},
components: {},
created() {
},
methods: {
downloadFiles(data,type){
this.JSONToExcelConvertor(data.content, type+"报表", data.title);
},
JSONToExcelConvertor(JSONData, FileName, ShowLabel) {
//先转化json
const arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
let excel = '<table>';
//设置表头
let row = "<tr>";
for (let i = 0, l = ShowLabel.length; i < l; i++) {
row += "<td>" + ShowLabel[i] + '</td>';
}
//换行
excel += row + "</tr>";
//设置数据
for (let i = 0; i < arrData.length; i++) {
let row = "<tr>";
for (let index in arrData[i]) {
const value = arrData[i][index] === "." ? "" : arrData[i][index];
row += '<td>' + value + '</td>';
}
excel += row + "</tr>";
}
excel += "</table>";
let excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelFile += '; charset=UTF-8">';
excelFile += "<head>";
excelFile += "<!--[if gte mso 9]>";
excelFile += "<xml>";
excelFile += "<x:ExcelWorkbook>";
excelFile += "<x:ExcelWorksheets>";
excelFile += "<x:ExcelWorksheet>";
excelFile += "<x:Name>";
excelFile += "{worksheet}";
excelFile += "</x:Name>";
excelFile += "<x:WorksheetOptions>";
excelFile += "<x:DisplayGridlines/>";
excelFile += "</x:WorksheetOptions>";
excelFile += "</x:ExcelWorksheet>";
excelFile += "</x:ExcelWorksheets>";
excelFile += "</x:ExcelWorkbook>";
excelFile += "</xml>";
excelFile += "<![endif]-->";
excelFile += "</head>";
excelFile += "<body>";
excelFile += excel;
excelFile += "</body>";
excelFile += "</html>";
const uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
const link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = FileName + ".xls";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
然后在页面中调用
<template>
<div>
<button @click="_sendDownloadFilesServer()">下载报表</button>
</div>
</template> <script>
import RecordServer from "@/service/record.service"
const recordServer = new RecordServer()
import downloadFileMixin from "@/mixins/downfile" //把上面的文件导入
export default{
data(){
return{ }
},
mixins:[downloadFileMixin],
methods:{
_sendDownloadFilesServer(){
recordServer.sendDownloadFilesServer({
onSuccess:(res)=>{
console.log(res)
this.downloadFiles(res,'火车') // 点击下载文件会下载名为火车报表的excel
},
onFailed:(err)=>{
console.log(err)
}
})
}
}
}
</script>
vue 把后台返回的json拼接成excel并下载的更多相关文章
- js循环处理后台返回的json数组
<script type="text/javascript"> function gongdan_search(elm){ var dangqian_value=$(e ...
- 后台返回的json数据传到前端页面并在页面的表格中填充
web前端页面: <table id="myTable" cellpadding="1" cellspacing="0" border ...
- 前台如何处理后台返回的json数据
后台返回的json数据格式: { "state": true, "data": { "id": 0, "name": & ...
- JS获取后台返回的JSON数据
问题:通过$.get从后台获取了一段json串{"id":"1","name":"ww"},然后要拿到这里面的id和na ...
- vue.js如何根据后台返回来的图片url进行图片下载
最近在做一个前端vue.js对接的功能模块时,需要实现一个下载图片的功能,后台返回来的是一串图片url,试了很多种方法,发现点击下载时出来的效果都是跳着到一个新的图片网页,后来经过一番琢磨,终于解决了 ...
- Ajax处理后台返回的Json数据
// 处理后台传来的Json字符串装换成Json对象 var dataJson = JSON.parse(data); // 此时可以从Json对象中取值 if(dataJson.result == ...
- 后台返回的json数据,前台需要修改对象的键值名,重新修改数据格式
//例如,后台返回的数据为: var json = { data: [ {value: 103.95, name: "法拉利"}, {value: 103.2, name: & ...
- [转]android中解析后台返回的json字符串
普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid":"100196","Showname&qu ...
- 关于eval()函数处理后台返回的json数据
对于服务器返回的JSON字符串,如果jquery异步请求没做类型说明,或者以字符串方式接受,那么需要做一次对象化处理,方式不是太麻烦,就是将该字符串放于eval()中执行一次.这种方式也适合以普通ja ...
随机推荐
- maven 私服中央库使用阿里云库
1.admin登录 进入remote repositories management 2. 设置地址
- C#获取局域网MAC地址
效果: 说明:获取本局域网的MAC地址(非本机的MAC地址) 代码: /// <summary> /// 获取网卡物理地址 /// </summa ...
- cnblogs博客迁移到hexo
cnblogs博客备份 备份地址:https://i.cnblogs.com/BlogBackup.aspx?type=1 备份文件为xml格式,打开备份文件,如下所示: <?xml versi ...
- vss安装及服务器端、客户端配置图文教程
安装VSS 一.双击setup. 之后点完成.就安装完了! 服务器端VSS配置 一.选择开始——所有程序——打开 一直下一步 到完成 二.然后再打开 说明:把这个enable rights and ...
- Linux samba 服务的配置
今天有个学生问我 samba 服务怎么配置,所以晚上特意研究一下怎么配置这个服务. 过程如下: sudo apt-get install samba samba-common // 安装 samba ...
- PHP——小尾巴之权限管理
流程: 在权限管理页面,默认显示用户的角色,更改复选框的按钮内容,可以改变角色,点击确定提交至数据库 在登陆之后,只显示该用户的角色所对应的权限 数据库: guanli.php <!DOCTYP ...
- 对session的操作
request.getSession().removeAttribute("amount");request.getSession().setAttribute("amo ...
- cookie,Session机制的本质,跨应用程序的session共享
目录:一.术语session二.HTTP协议与状态保持三.理解cookie机制四.理解session机制五.理解javax.servlet.http.HttpSession六.HttpSession常 ...
- SpringCloud架构设计
最近一直在针对SpringCloud框架做项目,从中踩了不少的坑,也渐渐梳理出了一些内容,由于SpringCloud作为一个全家桶,其中东西太多,所以这时候就要有所取舍,这里就想把自己比较常用组件及架 ...
- poj 1090:The Circumference of the Circle(计算几何,求三角形外心)
The Circumference of the Circle Time Limit: 2 Seconds Memory Limit: 65536 KB To calculate the c ...