码迷,mamicode.com
首页 > 其他好文 > 详细

angular8 导出excel文件

时间:2019-12-12 12:50:18      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:imp   ima   open   type   comment   this   xls   elf   json   

angular

技术图片

 

package

1、xlsx  

  npm install xlsx --save

2、file-saver

  npm install file-saver --save

  npm install @types/file-saver --save

3、实现导出多个sheet的数据

exportExcel() {
    import("xlsx").then(xlsx => {
      // const worksheet = xlsx.utils.json_to_sheet(this.getCars());
      console.log(this.BeautifuleValue);
      var all = [
        [],
        [],
        []
      ];
      this.BeautifuleValue.districtBeautifyAdjustedValue.forEach(element => {
        all[0].push(
          {
            ‘区县‘: this.districtName[element.customer],
            ‘美化系数‘: element.coefficient,
            ‘美化值‘: element.beautifyValue
          }
        );
      });
      this.BeautifuleValue.platBeautifyAdjustedValue.forEach(element => {
        all[1].push(
          {
            ‘平台‘: this.platName[element.plat],
            ‘美化系数‘: element.coefficient,
            ‘美化值‘: element.beautifyValue
          }
        );
      });
      this.BeautifuleValue.industryBeautifyAdjustedValue.forEach(element => {
        all[2].push(
          {
            ‘行业‘: element.industry,
            ‘美化系数‘: element.coefficient,
            ‘美化值‘: element.beautifyValue
          }
        );
      });
      const oneSheet = xlsx.utils.json_to_sheet(all[0]);
      const twoSheet = xlsx.utils.json_to_sheet(all[1]);
      const threeSheet = xlsx.utils.json_to_sheet(all[2]);
      const workbook = { Sheets: { ‘区县‘: oneSheet, ‘平台‘: twoSheet, ‘行业‘: threeSheet }, SheetNames: [‘区县‘, ‘平台‘, ‘行业‘] };
      const excelBuffer: any = xlsx.write(workbook, { bookType: ‘xlsx‘, type: ‘array‘ });
      this.saveAsExcelFile(excelBuffer, "美化值");
    });
  }

  saveAsExcelFile(buffer: any, fileName: string): void {
    import("file-saver").then(FileSaver => {
      let EXCEL_TYPE = ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8‘;
      let EXCEL_EXTENSION = ‘.xlsx‘;
      const data: Blob = new Blob([buffer], {
        type: EXCEL_TYPE
      });
      FileSaver.saveAs(data, fileName + ‘_export_‘ + new Date().getTime() + EXCEL_EXTENSION);
    });
  }

 





angular8 导出excel文件

标签:imp   ima   open   type   comment   this   xls   elf   json   

原文地址:https://www.cnblogs.com/shuangzikun/p/12028480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!