先封装一下生成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并下载的更多相关文章

  1. js循环处理后台返回的json数组

    <script type="text/javascript"> function gongdan_search(elm){ var dangqian_value=$(e ...

  2. 后台返回的json数据传到前端页面并在页面的表格中填充

    web前端页面: <table id="myTable" cellpadding="1" cellspacing="0" border ...

  3. 前台如何处理后台返回的json数据

    后台返回的json数据格式: { "state": true, "data": { "id": 0, "name": & ...

  4. JS获取后台返回的JSON数据

    问题:通过$.get从后台获取了一段json串{"id":"1","name":"ww"},然后要拿到这里面的id和na ...

  5. vue.js如何根据后台返回来的图片url进行图片下载

    最近在做一个前端vue.js对接的功能模块时,需要实现一个下载图片的功能,后台返回来的是一串图片url,试了很多种方法,发现点击下载时出来的效果都是跳着到一个新的图片网页,后来经过一番琢磨,终于解决了 ...

  6. Ajax处理后台返回的Json数据

    // 处理后台传来的Json字符串装换成Json对象 var dataJson = JSON.parse(data); // 此时可以从Json对象中取值 if(dataJson.result == ...

  7. 后台返回的json数据,前台需要修改对象的键值名,重新修改数据格式

    //例如,后台返回的数据为: var json = { data: [   {value: 103.95, name: "法拉利"}, {value: 103.2, name: & ...

  8. [转]android中解析后台返回的json字符串

    普通形式的:服务器端返回的json数据格式如下: {"userbean":{"Uid":"100196","Showname&qu ...

  9. 关于eval()函数处理后台返回的json数据

    对于服务器返回的JSON字符串,如果jquery异步请求没做类型说明,或者以字符串方式接受,那么需要做一次对象化处理,方式不是太麻烦,就是将该字符串放于eval()中执行一次.这种方式也适合以普通ja ...

随机推荐

  1. XVAG音频文件格式提取

    索尼的一种游戏音频格式,通过vgmstream工具可以提取 工具已经下载好:(包含dll包) http://files.cnblogs.com/files/hont/vgmstream-r1040-t ...

  2. ny33 蛇形填数

    蛇形填数 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 12 1 9 16 1 ...

  3. pyspark采用python3开发

    现在时间2017-04-17. python版本3.5支持pyspark python3.6目前还不支持 做法简单,只需要在bin/pyspark中增加 export PYSPARK_PYTHON=p ...

  4. java读properties文件 乱码

    java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 ...

  5. ajax——三级联动下拉列表框的优化(简化页面,用jquery插件代替原来页面代码,返回处理数据类型为"TEXT")

    数据库: 主页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  6. GUI的广泛应用是当今计算机发展的重大成就之一

    GUI的广泛应用是当今计算机发展的重大成就之一,它极大地方便了非专业用户的使用.人们从此不再需要死记硬背大量的命令,取而代之的是可以通过窗口.菜单.按键等方式来方便地进行操作.而嵌入式GUI具有下面几 ...

  7. Throw是一个语句,用来做抛出例外的功能

    当我们自己定义一个例外类的时候必须使其继承excepiton或者RuntimeException. Throw是一个语句,用来做抛出例外的功能. 而throws是表示如果下级方法中如果有例外抛出,那么 ...

  8. ]flexslider 中文文档 使用教程 参数手册

    [原创]flexslider 中文文档 使用教程 参数手册   要改前人用的flexslider功能,但苦于找不到详细的文档教程,折磨了好久……(所以我才说不爱乱用插件) 为了福利下之后也苦于这个问题 ...

  9. android从放弃到坚持放弃第二课(下)

    续第二课( 下) 续第二课 下 活动的生命周期 返回栈 活动状态 活动的生存期 体验活动的生命周期 活动被回收怎么办 活动的启动模式 standard singleTop singleTask sin ...

  10. ORACLE之常用FAQ V1.0

    [B]第一部分.SQL&PL/SQL[/B][Q]怎么样查询特殊字符,如通配符%与_[A]select * from table where name like 'A\_%' escape ' ...