likes
comments
collection
share

使用exceljs导出表格文件-简易版

作者站长头像
站长
· 阅读数 28

引入所需依赖

import Excel from 'exceljs';
import {saveAs} from 'file-saver';

创建工作簿并写入内容

const workbook = new Excel.Workbook();
const worksheet = workbook.addWorksheet('My Sheet');
worksheet.columns = [
  { header: '编号', key: 'id', width: 10 },
  { header: '姓名', key: 'name', width: 32 },
  { header: '年龄', key: 'age', width: 10 }
];

var rows = [
  ['001','MIKA','23'], // row by array
  {id:'002', name: 'Kaz', age:'24'}
];

worksheet.addRows(rows);

利用 file-saver的saveAs保存至本地

const fileType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
workbook.xlsx.writeBuffer().then(data=>{
  const blob = new Blob([data],{type:fileType});
  saveAs(blob,'test.xlsx');
})

进行到这一步,基本上就已经实现了保存excel表格到本地的需求。参考:exceljs官方文档

转载自:https://segmentfault.com/a/1190000041682837
评论
请登录