Commands to install:
=============================
npm install file-saver
npm install xlsx
Generate a service named excel
==============================
ng g service excel
Imports in excel service
==============================
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()
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: '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() + EXCEL_EXTENSION);
}
App Component.ts
==========================================
import {ExcelService} from './services/excel.service';
data: any = [
{
"CATEGORYID": 1,
"CATEGORYNAME": "BOOKS",
"DESCRIPTION": "It contains all types of books",
"IMAGE": "Books",
"STATUS": "TRUE"
},
{
"CATEGORYID": 2,
"CATEGORYNAME": "EBOOKS",
"DESCRIPTION": "It contains all types of ebooks",
"IMAGE": "Ebooks",
"STATUS": "TRUE"
},
{
"CATEGORYID": 3,
"CATEGORYNAME": "Bookss",
"DESCRIPTION": "DESCRIPTION",
"IMAGE": "IMAGE",
"STATUS": "TRUE"
}
]
constructor(private excelService:ExcelService){}
exportAsXLSX():void {
this.excelService.exportAsExcelFile(this.data, 'myExcelFile');
}
=============================
npm install file-saver
npm install xlsx
Generate a service named excel
==============================
ng g service excel
Imports in excel service
==============================
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()
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: '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() + EXCEL_EXTENSION);
}
App Component.ts
==========================================
import {ExcelService} from './services/excel.service';
data: any = [
{
"CATEGORYID": 1,
"CATEGORYNAME": "BOOKS",
"DESCRIPTION": "It contains all types of books",
"IMAGE": "Books",
"STATUS": "TRUE"
},
{
"CATEGORYID": 2,
"CATEGORYNAME": "EBOOKS",
"DESCRIPTION": "It contains all types of ebooks",
"IMAGE": "Ebooks",
"STATUS": "TRUE"
},
{
"CATEGORYID": 3,
"CATEGORYNAME": "Bookss",
"DESCRIPTION": "DESCRIPTION",
"IMAGE": "IMAGE",
"STATUS": "TRUE"
}
]
constructor(private excelService:ExcelService){}
exportAsXLSX():void {
this.excelService.exportAsExcelFile(this.data, 'myExcelFile');
}
Can it work for mobile app ?
ReplyDeletedo you mean with react native?
DeleteFunciona en app móvil en ionic o solo en la web..?
ReplyDelete