adnanmc / ngx-excel-export

Angular4 application with export data to excel file functionality.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Description

An example Angular4 application that shows how to export data to an excel file.

Instruction

Start application

Simple start application by executing ng serve command.

Use in your project

Follow this instruction if you want to export data to excel file in your project.

  1. Add file-saver and xlsx dependencies
npm install file-saver --save
npm install xlsx --save
  1. Implement ExcelService
import { Injectable } from '@angular/core';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';

const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
const EXCEL_EXTENSION = '.xlsx';

@Injectable()
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: { 'data': worksheet }, SheetNames: ['data'] };
    const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer' });
    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() + EXCEL_EXTENSION);
  }

}

About

Angular4 application with export data to excel file functionality.


Languages

Language:TypeScript 74.7%Language:JavaScript 14.3%Language:HTML 10.3%Language:CSS 0.7%