1.先安装依赖
npm install --save xlsx file-saver
2.在需要的组件内引入
import FileSaver from “file-saver”;
import XLSX from “xlsx”
3、案例
现在需要导出的表格添加 class名称
然后走接口,成功后走这段代码
var wb = XLSX.utils.table_to_book(document.querySelector(".table"));
/* get binary string as output */
var wbout = XLSX.write(wb, {
bookType: "xlsx",
bookSST: true,
type: "array"
});
try {
// name+'.xlsx'表示导出的excel表格名字
FileSaver.saveAs(
new Blob([wbout], { type: "application/octet-stream" }),
name + ".xlsx"
);
} catch (e) {
if (typeof console !== "undefined") console.log(e, wbout);
}
return wbout;
效果图