angular8 导出excel文件

angular

angular8 导出excel文件

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);
    });
  }
 

相关推荐