使用到的模塊: exceljs
var Excel = require('exceljs');
var start_time = new Date();
var workbook = new Excel.stream.xlsx.WorkbookWriter({
filename: './streamed-workbook.xlsx'
});
var worksheet = workbook.addWorksheet('Sheet');
worksheet.columns = [
{ header: 'id', key: 'id' },
{ header: 'name', key: 'name' },
{ header: 'phone', key: 'phone' }
];
var data = [{
id: 100,
name: 'abc',
phone: '123456789'
}];
var length = data.length;
// 當前進度
var current_num = 0;
var time_monit = 400;
var temp_time = Date.now();
console.log('開始添加數(shù)據(jù)');
// 開始添加數(shù)據(jù)
for(let i in data) {
worksheet.addRow(data[i]).commit();
current_num = i;
if(Date.now() - temp_time > time_monit) {
temp_time = Date.now();
console.log((current_num / length * 100).toFixed(2) + '%');
}
}
console.log('添加數(shù)據(jù)完畢:', (Date.now() - start_time));
workbook.commit();
var end_time = new Date();
var duration = end_time - start_time;
console.log('用時:' + duration);
console.log("程序執(zhí)行完畢");