大多数excel表格的导出,直接一个a标签跳转就行了
但是为了安全考虑,有些公司前端需要用post方式获取后端返回的文件流,前端使用node将文件流转译后再导出
下面就是代码
exportBtn(){
//导出修改为读取二进制文件流
let parm = {
report_type: this.sheetType,
set_id: this.searchName,
selectDate: selectDate,
startTime: startTime,
endTime: endTime,
};
api_tws.excelReport(qs.stringify(parm)).then((res) => {
if (res == "apiError") {
this.buttonDisabledType = false;
return;
}
const blob = new Blob([res]);
const elink = document.createElement("a");
elink.download = `报表.xls`;
elink.style.display = "none";
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href); // 释放URL 对象
document.body.removeChild(elink);
this.buttonDisabledType = false;
});
}
//上面是方法,下面是定义的api请求
export async function excelReport(body) {
const res = await axios({
method: "post",
responseType: "blob",
url: serverUrl + '/excelReport/export?' + body
});
return res;
}