angular8 导出excel文件
程序员文章站
2022-08-08 15:33:10
angular package 1、xlsx npm install xlsx --save 2、file-saver npm install file-saver --save npm install @types/file-saver --save 3、实现导出多个sheet的数据 export ......
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); }); }