(办公)vue下载excel,后台用post方法
程序员文章站
2023-04-05 22:50:56
后台方法的参数必须是@RequestBody修饰的。 前台关键代码: ......
后台方法的参数必须是@requestbody修饰的。
前台关键代码:
axios ( { method : 'post', url : api.exportplaytime , // 请求地址 data : { choose : type, begindate : startdate, enddate : enddate }, responsetype : 'arraybuffer', observe: 'response', } ) .then ( ( res ) => { const filename = ""+filename+".xlsx" let blob = new blob([res.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}); if ( 'download' in document.createelement ( 'a' ) ) { // 非ie下载 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 ) } else { // ie10+下载 navigator.mssaveblob ( blob, filename ) } })
download(data) { if (!data) { return } let url = window.url.createobjecturl(new blob([data])) let link = document.createelement('a') link.style.display = 'none' link.href = url link.setattribute('download', 'excel.xlsx') document.body.appendchild(link) link.click() },