egg.js下载导出excel文件

    const xlsx = require(''xlsx)
    const data = [{name: 'hello',age:1},{name: 'world',age:2}]
    const jsonWorkSheet = xlsx.utils.json_to_sheet(data);
    const workBook = {
      SheetNames: [ 'Sheet0' ],
      Sheets: {
        Sheet0: jsonWorkSheet,
      },
    };
    const filename = `report_${fecha.format(new Date(), 'YYYYMMDDhhmmss')}.xlsx`;
    const filePath = path.resolve(this.app.config.baseDir, filename);
    await xlsx.writeFile(workBook, filePath);
    ctx.attachment(filename);
    ctx.body = fs.createReadStream(filePath);
    fs.unlink(filePath, err => {
      if (err) throw err;
      // console.log('excel文件已被删除');
    });
posted @ 2020-06-18 17:33  樱风凛  阅读(1599)  评论(0编辑  收藏  举报