export datatable to excel angular 7

Solutions on MaxInterview for export datatable to excel angular 7 by the best coders in the world

showing results for - "export datatable to excel angular 7"
Soline
03 Aug 2018
1import { Component } from '@angular/core';
2import { ExcelService } from './services/excel.service';
3
4@Component({
5  selector: 'app-root',
6  templateUrl: './app.component.html',
7  styleUrls: ['./app.component.css']
8})
9export class AppComponent {
10  title = 'exportExcelInAngular';
11  dataOfFootballers: any = [{
12    playerName: 'Cristiano Ronaldo',
13    playerCountry: 'Pourtgal',
14    playerClub: 'Juventus'
15  },
16  {
17    playerName: 'Lionel Messi',
18    playerCountry: 'Argentina',
19    playerClub: 'Barcelona'
20  },
21  {
22    playerName: 'Neymar Junior',
23    playerCountry: 'Brazil',
24    playerClub: 'PSG'
25  },
26  {
27  playerName: 'Tonni Kroos',
28  playerCountry: 'Germany',
29  playerClub: 'Real Madrid'
30  },
31  {
32    playerName: 'Paul Pogba',
33    playerCountry: 'France',
34    playerClub: 'Manchester United'
35  }];
36  constructor(private excelService:ExcelService){
37
38  }
39  exportAsXLSX():void {
40    this.excelService.exportAsExcelFile(this.dataOfFootballers, 'footballer_data');
41  }
42}
43
Helena
31 Jun 2019
1import { Injectable } from '@angular/core';
2import * as FileSaver from 'file-saver';
3import * as XLSX from 'xlsx';
4
5const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
6const EXCEL_EXTENSION = '.xlsx';
7
8@Injectable()
9export class ExcelService {
10
11  constructor() { }
12
13  public exportAsExcelFile(json: any[], excelFileName: string): void {
14    
15    const myworksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
16    const myworkbook: XLSX.WorkBook = { Sheets: { 'data': myworksheet }, SheetNames: ['data'] };
17    const excelBuffer: any = XLSX.write(myworkbook, { bookType: 'xlsx', type: 'array' });
18    this.saveAsExcelFile(excelBuffer, excelFileName);
19  }
20
21  private saveAsExcelFile(buffer: any, fileName: string): void {
22    const data: Blob = new Blob([buffer], {
23      type: EXCEL_TYPE
24    });
25    FileSaver.saveAs(data, fileName + '_exported'+ EXCEL_EXTENSION);
26  }
27
28}