这里使用的excel-export来导出表格, 使用前需要安装
const nodeExcel = require('excel-export');
router.get('/exportexcel',async (ctx) => {
async function readydata() {
获取数据
let exceldata = 填充的数据;
return exceldata;
}
//导出
async function exportdata(v) {
// 配置数据的展示
let conf ={};
conf.name = "mysheet";//表格名
let alldata = new Array();
for(let i = 0;i<v.length;i++){
let arr = new Array();
arr.push(v[i].type);
arr.push(v[i].trade_no);
arr.push(v[i].pay_address);
arr.push(v[i].collection_address);
arr.push(v[i].currency);
arr.push(v[i].slk);
arr.push(v[i].poundage);
arr.push(v[i].real_account);
arr.push(v[i].status);
arr.push(v[i].trad_time);
alldata.push(arr);
}
//决定列名和类型
conf.cols = [表格数据];
// 获取时间戳,用来定义文件名
const date = new Date().getTime()
conf.rows = alldata;//填充数据
let result = nodeExcel.execute(conf);
let data = new Buffer(result,'binary');
ctx.set('Content-Type', 'application/vnd.openxmlformats');
ctx.set("Content-Disposition", "attachment; filename=" + date+".xlsx");
ctx.body=data;
}
let r=await readydata();
await exportdata(r);
});