1.首先使用自己封装的接口请求
记住不要忘记加: responseType: “arraybuffer” ,
export function exportNoticemsg(query) {
return request({
url: '/agent/noticemsg/export',
method: 'get',
responseType: "arraybuffer" ,
params: query
})
}
2.在页面中使用
//引入接口
import { exportNoticemsg } from "@/api/device/noticemsg";
exportNoticemsg(queryParams).then(res=>{
console.log(res);
const blob = new Blob([res], { // res 为后台返回数据
type: 'application/vnd.ms-excel;charset=utf-8', // 导出数据生成excel的格式设置
});
console.log(blob);
const elink = document.createElement('a');
elink.download = `表格导出.xlsx`;
elink.style.display = 'none';
elink.target = '_blank';
elink.href = window.URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
})