使用 js-export-excel 插件如蚜,github地址點擊查看
npm install js-export-excel
exportDate() {
// 直接導(dǎo)出文件
const ExportJsonExcel = require("js-export-excel");
var option = {};
option.fileName = "表格數(shù)據(jù)"; // 表格名稱
const header = [ '姓名', '年齡' ]
const data = [{ name: '李三', age: 12 }, { name: '李四', age: 14 }];
const dataKey = [ 'name', 'age' ];
const colWith = new Array(dataKey.length).fill(5);
option.datas = [
{
sheetData: data, // 表格數(shù)據(jù)
sheetName: "數(shù)據(jù)", // 表格里sheet名
sheetFilter: dataKey, // 表格數(shù)據(jù)key
sheetHeader: header, // 表頭
columnWidths: colWith, // 寬度
},
];
var toExcel = new ExportJsonExcel(option); //new
toExcel.saveExcel(); //保存
},