背景
最近在应用 electon 写一个桌面程序,须要对数据进行 excel 导出操作,这里抉择应用 exceljs
来实现这个性能,上面是 exceljs 导出性能的简略实现。
性能实现
下载 exceljs
yarn add exceljs
引入 exceljs
const ExcelJS = require('exceljs');
创立工作簿并设置工作簿属性
const workbook = new ExcelJS.Workbook();
workbook.creator = 'Me';
workbook.lastModifiedBy = 'Her';
workbook.created = new Date(1985, 8, 30);
workbook.modified = new Date();
workbook.lastPrinted = new Date(2016, 9, 27);
增加工作表
const sheet = workbook.addWorksheet('My Sheet');
工作表就是装载 excel 表外面数据的容器
往工作表增加数据
设置表头
worksheet.columns = [{ header: 'Id', key: 'id', width: 10},
{header: 'Name', key: 'name', width: 32},
{header: 'D.O.B.', key: 'DOB', width: 10}
];
增加数据
const data = [[1, 'electron','DOB']
[2, 'exceljs', 'DOB]
];
worksheet.addRows(data);
导出 xlsx 文件
await workbook.xlsx.writeFile(filename);
所有代码
const ExcelJS = require('exceljs');
const excelExport = async () => {const workbook = new ExcelJS.Workbook();
workbook.creator = 'Me';
workbook.lastModifiedBy = 'Her';
workbook.created = new Date(1985, 8, 30);
workbook.modified = new Date();
workbook.lastPrinted = new Date(2016, 9, 27);
const worksheet = workbook.addWorksheet('My Sheet');
worksheet.columns = [{ header: 'Id', key: 'id', width: 10},
{header: 'Name', key: 'name', width: 32},
{header: 'D.O.B.', key: 'DOB', width: 10}
];
const data = [[1, 'electron','DOB'],
[2, 'exceljs', 'DOB']
];
worksheet.addRows(data);
await workbook.xlsx.writeFile(fileName);
}
excelExport();
参考资料
exceljs 官网文档
exceljs 官网中文文档