前端處理二進(jìn)制流文件導(dǎo)出為excel表代碼示例
更新時間:2023年08月03日 11:07:09 作者:劉小蟲_
這篇文章主要給大家介紹了關(guān)于前端處理二進(jìn)制流文件導(dǎo)出為excel表的相關(guān)資料,后臺管理系統(tǒng),常會出現(xiàn)導(dǎo)出excel表格功能,需要的朋友可以參考下
將后端返回的二進(jìn)制流文件
導(dǎo)出為excel表用的時候直接調(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript setTimeout()基本用法有哪些
這篇文章主要介紹了JavaScript setTimeout()基本用法有哪些,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11JavaScript基于面向?qū)ο髮崿F(xiàn)的猜拳游戲
這篇文章主要介紹了JavaScript基于面向?qū)ο髮崿F(xiàn)的猜拳游戲,結(jié)合完整實例形式分析了javascript基于面向?qū)ο髮崿F(xiàn)猜拳游戲的具體頁面布局、樣式及功能相關(guān)操作技巧,需要的朋友可以參考下2018-01-01javascript將浮點數(shù)轉(zhuǎn)換成整數(shù)的三個方法
將浮點數(shù)轉(zhuǎn)換成整數(shù)方法有很多,本例為大家介紹常用的三個方法,如果讀者想到其他好用方法,也可以交流一下2014-06-06javascript算法解數(shù)獨實現(xiàn)方案示例
這篇文章主要為大家介紹了javascript算法解數(shù)獨實現(xiàn)方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08