vue下载xlsx表格
// 导入依赖库
import XLSX from 'xlsx';
import FileSaver from 'file-saver';
methods:{
btn(){
let date = new Date()
let Y = date.getFullYear() + '-'
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
var dates='体检接口_'+Y + M + D
// 构建表格数据
const header = ['序号', '系统编码', '系统名称', '编码', '名称', '单位', '单位转换','转换参数',];
const data1 = this.rightArr.map((item,index) => [index+1, item.sysCode, item.sysName,item.code,item.name,item.unit,'','']);
const worksheet = XLSX.utils.aoa_to_sheet([header, ...data1]);
// 创建工作簿并保存到本地
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
// const date = new Date().toLocaleDateString();
const fileName = `${dates}.xlsx`;//表格名称
const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });
FileSaver.saveAs(blob, fileName);
}
}