前端處理二進(jìn)制流文件導(dǎo)出為excel表代碼示例
將后端返回的二進(jìn)制流文件
導(dǎo)出為excel表用的時(shí)候直接調(diào)用showConfirm函數(shù)即可
最后效果

代碼示例
export function getExport(param) {
get('/api/xdata/v1/basic/auth/excel', { ...param }).then((res) => {
// let name = getFileName(url);
let name = 'export.xlsx';
console.log('res', res);
// let u = window.URL.createObjectURL(new Blob([res]));
const type = 'application/vnd.ms-excel;charset=utf-8'; //excel文件
let u = window.URL.createObjectURL(new Blob([res], { type: type }));
let a = document.createElement('a');
a.download = name;
a.href = u;
console.log(a);
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
// setTimeout(myDlWarn, 0);
});
}
export function showConfirm(text, exportParams) {
confirm({
title: `您確認(rèn)要導(dǎo)出${text}嗎`,
icon: <ExclamationCircleOutlined />,
content: '',
okText: '確認(rèn)',
okType: 'primary',
cancelText: '取消',
onOk() {
getExport(exportParams);
},
onCancel() {
console.log('Cancel');
},
});
}get接口是自己封裝的,封裝如下
/**
* 從 cookie 中獲取數(shù)據(jù)
* @param {string} cname cname
*/
export const getCookie = (cname) => {
var name = cname + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
};
const ajax = (method, url, data, options = {}) => {
const isPost = method === 'post';
const isPut = method === 'put';
const isPatch = method === 'patch';
const isGet = method === 'get';
const sentOptions = {
url,
method,
withCredentials: true, // 允許跨域
credentials: 'include',
headers: {
'X-Request-By': 'ERApplication',
'X-Requested-With': 'XMLHttpRequest',
'X-Region': 'bj',
'X-From': 'web',
},
...options,
};
if (isPost || isPatch) {
sentOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded';
sentOptions.data = JSON.stringify(data);
} else if (isPut) {
sentOptions.data = JSON.stringify(data);
} else if (isGet) {
sentOptions.headers['Content-Type'] = 'utf-8';
sentOptions.params = data;
}
return new Promise((resolve, reject) => {
axios(sentOptions)
.then((response) => {
resolve(response.data);
.catch((error) => {
console.log('catch');
reject(error);
});
});
};
export const get = (url, data = {}) => {
return ajax('get', BASE_URL + url, data, { responseType: 'blob' });
};總結(jié)
到此這篇關(guān)于前端處理二進(jìn)制流文件導(dǎo)出為excel表的文章就介紹到這了,更多相關(guān)前端二進(jìn)制流導(dǎo)出excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序三級(jí)聯(lián)動(dòng)選擇器使用方法
這篇文章主要為大家詳細(xì)介紹了微信小程序三級(jí)聯(lián)動(dòng)選擇器使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
JavaScript setTimeout()基本用法有哪些
這篇文章主要介紹了JavaScript setTimeout()基本用法有哪些,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
OpenLayer學(xué)習(xí)之自定義測(cè)量控件
這篇文章主要為大家詳細(xì) 介紹了OpenLayer學(xué)習(xí)之自定義測(cè)量控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
JavaScript基于面向?qū)ο髮?shí)現(xiàn)的猜拳游戲
這篇文章主要介紹了JavaScript基于面向?qū)ο髮?shí)現(xiàn)的猜拳游戲,結(jié)合完整實(shí)例形式分析了javascript基于面向?qū)ο髮?shí)現(xiàn)猜拳游戲的具體頁(yè)面布局、樣式及功能相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
javascript將浮點(diǎn)數(shù)轉(zhuǎn)換成整數(shù)的三個(gè)方法
將浮點(diǎn)數(shù)轉(zhuǎn)換成整數(shù)方法有很多,本例為大家介紹常用的三個(gè)方法,如果讀者想到其他好用方法,也可以交流一下2014-06-06
javascript算法解數(shù)獨(dú)實(shí)現(xiàn)方案示例
這篇文章主要為大家介紹了javascript算法解數(shù)獨(dú)實(shí)現(xiàn)方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
js實(shí)現(xiàn)一個(gè)猜數(shù)字游戲
本文主要介紹了js實(shí)現(xiàn)一個(gè)猜數(shù)字游戲的實(shí)例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-03-03

