UNPKG

file-saver-angular

Version:

This library is used to export data to csv/excel

135 lines (128 loc) 5.23 kB
import { __decorate } from 'tslib'; import { ɵɵdefineInjectable, Injectable, Component, NgModule } from '@angular/core'; var FileSaverAngularService = /** @class */ (function () { function FileSaverAngularService() { } FileSaverAngularService.prototype.exportToCsv = function (rows, filename) { this.csvExport(rows, filename); }; FileSaverAngularService.prototype.csvExport = function (rows, filename) { if (!rows || !rows.length) { return; } var separator = ','; var keys = Object.keys(rows[0]); var csvContent = keys.join(separator) + '\n' + rows.map(function (row) { return keys.map(function (k) { var cell = row[k] === null || row[k] === undefined ? '' : row[k]; cell = cell instanceof Date ? cell.toLocaleString() : cell.toString().replace(/"/g, '""'); if (cell.search(/("|,|\n)/g) >= 0) { cell = "\"" + cell + "\""; } return cell; }).join(separator); }).join('\n'); var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' }); if (navigator.msSaveBlob) { // IE 10+ navigator.msSaveBlob(blob, filename); } else { var link = document.createElement('a'); if (link.download !== undefined) { // Browsers that support HTML5 download attribute var url = URL.createObjectURL(blob); link.setAttribute('href', url); link.setAttribute('download', filename); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } } }; FileSaverAngularService.prototype.exportToExcel = function (rows, filename) { this.csvExport(rows, filename); }; FileSaverAngularService.prototype.excelExport = function (rows, filename) { if (!rows || !rows.length) { return; } var separator = ','; var keys = Object.keys(rows[0]); var csvContent = keys.join(separator) + '\n' + rows.map(function (row) { return keys.map(function (k) { var cell = row[k] === null || row[k] === undefined ? '' : row[k]; cell = cell instanceof Date ? cell.toLocaleString() : cell.toString().replace(/"/g, '""'); if (cell.search(/("|,|\n)/g) >= 0) { cell = "\"" + cell + "\""; } return cell; }).join(separator); }).join('\n'); var blob = new Blob([csvContent], { type: 'application/xls;charset=utf-8;' }); if (navigator.msSaveBlob) { // IE 10+ navigator.msSaveBlob(blob, filename); } else { var link = document.createElement('a'); if (link.download !== undefined) { // Browsers that support HTML5 download attribute var url = URL.createObjectURL(blob); link.setAttribute('href', url); link.setAttribute('download', filename); link.style.visibility = 'hidden'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } } }; FileSaverAngularService.ɵprov = ɵɵdefineInjectable({ factory: function FileSaverAngularService_Factory() { return new FileSaverAngularService(); }, token: FileSaverAngularService, providedIn: "root" }); FileSaverAngularService = __decorate([ Injectable({ providedIn: 'root' }) ], FileSaverAngularService); return FileSaverAngularService; }()); var FileSaverAngularComponent = /** @class */ (function () { function FileSaverAngularComponent() { } FileSaverAngularComponent.prototype.ngOnInit = function () { }; FileSaverAngularComponent = __decorate([ Component({ selector: 'lib-file-saver-angular', template: "\n <p>\n file-saver-angular works!\n </p>\n " }) ], FileSaverAngularComponent); return FileSaverAngularComponent; }()); var FileSaverAngularModule = /** @class */ (function () { function FileSaverAngularModule() { } FileSaverAngularModule = __decorate([ NgModule({ declarations: [FileSaverAngularComponent], imports: [], exports: [FileSaverAngularComponent] }) ], FileSaverAngularModule); return FileSaverAngularModule; }()); /* * Public API Surface of file-saver-angular */ /** * Generated bundle index. Do not edit. */ export { FileSaverAngularComponent, FileSaverAngularModule, FileSaverAngularService }; //# sourceMappingURL=file-saver-angular.js.map