第一步:npm install xlsx file-saver -S
第二部:import * as XLXS from "xlsx";
import FileSaver from "file-saver";
第三步使用:
const exportToExcel = () => {
// 通过id,获取导出的表格数据。此时获取的id为结构中表单组件的id
const wb = XLXS.utils.table_to_book(document.getElementById("table"), {
raw: true,
});
const wbout = XLXS.write(wb, {
bookType: "xlsx",
bookSST: true,
type: "array",
});
try {
FileSaver.saveAs(new Blob([wbout], {
// 定义文件格式流
type: "application/octet-stream",
}),
name.value + ".xlsx"
);
} catch (e) {
console.log(e);
}
return wbout;
}