安装
npm install --save xlsx
import * as XLSX from 'xlsx';
exportArrayData: [
["序号", "英文名", "中文名"],
["1", "Good Morning", "早上好"],
["2", "Good Morning", "早上好"],
["3", "Good Morning", "早上好"],
["4", "Good Morning", "早上好"],
["5", "Good Morning", "早上好"]
]
exportArrayDataToExcel() {
const wb = XLSX.utils.book_new(); // 创建工作簿
const ws = XLSX.utils.aoa_to_sheet(this.exportArrayData); // 创建工作表
const cols = [];
// 设置列宽
for (let i = 0; i < this.exportArrayData[0].length; i++) {
cols.push({ wch: 15 });
}
ws['!cols'] = cols;
XLSX.utils.book_append_sheet(wb, ws, 'exportArrayDataToExcel');
// 导出Excel
XLSX.writeFile(wb, '表格.xlsx');
},
参考:
Vue+File saver+xlsx使用aoa_to_sheet导出数据为excel