You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
914 B
TypeScript
29 lines
914 B
TypeScript
|
3 weeks ago
|
import { Injectable } from '@angular/core';
|
||
|
|
import { saveAs } from 'file-saver';
|
||
|
|
import * as XLSX from 'xlsx';
|
||
|
|
import { EXCEL_FILE_EXTENSION, EXCEL_FILE_TYPE } from '../../utils/app.constants';
|
||
|
|
|
||
|
|
@Injectable({
|
||
|
|
providedIn: 'root'
|
||
|
|
})
|
||
|
|
export class ExcelExportService {
|
||
|
|
|
||
|
|
constructor() { }
|
||
|
|
|
||
|
|
private fileType = EXCEL_FILE_TYPE;
|
||
|
|
private fileExtension = EXCEL_FILE_EXTENSION;
|
||
|
|
|
||
|
|
public exportExcel(jsonData: any[], fileName: string): void {
|
||
|
|
|
||
|
|
const ws: XLSX.WorkSheet = XLSX.utils.json_to_sheet(jsonData);
|
||
|
|
const wb: XLSX.WorkBook = { Sheets: { 'data': ws }, SheetNames: ['data'] };
|
||
|
|
const excelBuffer: any = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
|
||
|
|
this.saveExcelFile(excelBuffer, fileName);
|
||
|
|
}
|
||
|
|
|
||
|
|
private saveExcelFile(buffer: any, fileName: string): void {
|
||
|
|
const data: Blob = new Blob([buffer], {type: this.fileType});
|
||
|
|
saveAs.saveAs(data, fileName + this.fileExtension);
|
||
|
|
}
|
||
|
|
}
|