angular+excelJs结合file-saver

exceljs文档
FileSaver文档(用于保存文件下载)

// 前端excel模板示例

import * as ExcelJS from  exceljs ;
import * as fs from  file-saver ;

  async exportExcel() {
    const workbook = new ExcelJS.Workbook();
    const worksheet = workbook.addWorksheet( My Sheet ,
      {
        properties: {
          tabColor: { argb:  ff0000  },
          defaultColWidth: 20,
          defaultRowHeight: 20,
        }, views: [{ state:  frozen , xSplit: 1, ySplit: 1 }]
      });

    // 设置列
     worksheet.columns = [
       { header:  Id , key:  id , },
       { header:  Name , key:  name , },
       { header:  D.O.B. , key:  DOB , }
     ];

    let tableDate = [{ id: 1, name:  John Doe , DOB:  2022-06-18  }, { id: 2, name:  Jane Doe , DOB:  2022-06-18  }];

   // worksheet.columns = this.excelSheet;
    // 插入数据
    worksheet.addRows(tableDate);

    // 写入文件
    const buffer = await workbook.xlsx.writeBuffer();
    fs.saveAs(new Blob([buffer], {
      type:  application/vnd.openxmlformats-officedocument.spreadsheetml.sheet , // 示例时可去掉type
    }), `Equity Risk Rating - ${DateFormatter.simpleDateFormatter(new Date())}.xlsx`)

  }

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
none
暂无评论...