Advertisement
Ahlushko

Untitled

Jun 23rd, 2020
1,029
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import { LabService } from '@services'
  3. @Component({
  4.     selector: 'app-root',
  5.     templateUrl: './app.component.html',
  6.     styleUrls: ['./app.component.css']
  7. })
  8. export class AppComponent {
  9.     public xl;
  10.     constructor(public lab: LabService) {
  11.     }
  12.     export() {
  13.         this.lab.export().subscribe((res:any) => {
  14.             this.xl = JSON.stringify(res)
  15.         })
  16.     }
  17.     exportTableToExcel(tableID, filename = ''){
  18.         var downloadLink;
  19.         var dataType = 'application/vnd.ms-excel';
  20.         var tableSelect = document.getElementById(tableID);
  21.         var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
  22.         filename = filename?filename+'.xls':'excel_data.xls';
  23.         downloadLink = document.createElement("a");
  24.         document.body.appendChild(downloadLink);
  25.         if(navigator.msSaveOrOpenBlob){
  26.             var blob = new Blob(['\ufeff', tableHTML], {
  27.                 type: dataType
  28.             });
  29.             navigator.msSaveOrOpenBlob( blob, filename);
  30.         }else{
  31.             downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
  32.             downloadLink.download = filename;
  33.             downloadLink.click();
  34.         }
  35.     }
  36.     ngOnInit(): void {
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement