Creation of XLS from Angular TypeScript [closed]


Good morning,

I need to export a table with an angular TypeScript Html to an xls format file.

The structure of the table is as follows.

<!-- Inicio de contenido de la tabla -->
           <table class="table table-hover table-striped  table-sm table-bordered">
                <tr  class="thead-dark">
                    <th>Nombre 1</th>
                    <th>Nombre 2</th>
                    <th>Apellido 1</th>
                    <th>Apellido 2</th>
                    <th>Asesor Comercial</th>
                    <th>Fecha Creación</th>
                    <th>Ultima Modificacion</th>
                <tr *ngFor="let op of oportunidades">

                      <div class="btn-group float-right" role="group" aria-label="Basic example">
                          <p-button label="Notas" icon="fa fa-check" iconPos="left" (click)="verNota(template,"></p-button>


asked by Jorge Reinaldo Linares Pineda 18.12.2018 в 17:27

1 answer


After trying I was able to solve my problem with the following line of code,

import { Injectable } from '@angular/core';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
const EXCEL_TYPE = 'application/vnd.openxmlformats- 
const EXCEL_EXTENSION = '.xlsx';

   providedIn: 'root'
export class ExcelService {
  constructor() { }
public exportAsExcelFile(json: any[], excelFileName: string): void {
    const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
    const workbook: XLSX.WorkBook = { Sheets: { 'homologacion': worksheet }, 
SheetNames: ['homologacion'] };
    const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' 
    this.saveAsExcelFile(excelBuffer, excelFileName);
private saveAsExcelFile(buffer: any, fileName: string): void {
    const data: Blob = new Blob([buffer], { type: EXCEL_TYPE });
    FileSaver.saveAs(data, fileName + '_export_' + new Date().getTime() + 

This file receives an object of any type and creates the xls file to download

answered by 20.12.2018 / 16:06