1.首先安装依赖
npm install --save xlsx npm install --save file-saver npm install --save xlsx-style
2.引入
import { utils, write, read } from 'xlsx'; import * as XLSX from 'xlsx';
3.写一个这样的函数
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'); };
4.点击时绑定表格id,然后声明一个FormDate,这个obj可以当做参数使用
const excel = getExcel(document.getElementById('idd'), '名称'); const obj = new FormData(); obj.append('excel', excel); obj.append('id', 1);