下载依赖:
npm run install xlsx
npm install file-saver -S
引入:
import * as XLSX from 'xlsx';
import FileSaver from 'file-saver';
正常:
导出下载:
const exData = (dom, title) => { let wb = XLSX.utils.table_to_book(dom, { raw: true }); let wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array', }); try { FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), title + `.xlsx`); } catch (e) { if (typeof console !== 'undefined') { console.log(e, wbout); } } return wbout; }; exData(document.querySelector('#idsssx'), '薪资表');
导出不下载:
let datasses = datass.value + '-' + datas.value; const getExcel = (tableRef, excelTitle) => { const wb = XLSX.utils.table_to_book(tableRef, { raw: true }); const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array', }); return new File([wbout], excelTitle + '.xlsx'); }; const excel = getExcel(document.getElementById('baga'), '薪资表'); console.log(excel); const obj = new FormData(); obj.append('excel', excel); obj.append('nowdate', datasses); obj.append('projectid', 6); obj.append('companyid', 2); axios({ method: 'POST', url: '/wage/index/batch_add', data: obj, }).then((res) => { console.log(res); if (res.data.code == 2) { ElMessage({ message: '添加失败,请检查未填写的地方', type: 'warning', }); } else { ElMessage({ showClose: true, message: '添加成功', type: 'success', }); addition.value = 0; } });