import { Component, OnInit, ViewChild } from
'@angular/core'
;
import { ProductService } from
'./productservice'
;
import { Table } from
'primeng/table'
;
import * as FileSaver from
'file-saver'
;
@Component({
selector:
'app-root'
,
templateUrl:
'./app.component.html'
,
})
export class AppComponent {
tutorials: Tutorial[];
@ViewChild(
'dt'
) table: Table;
constructor(private productService: ProductService) { }
cols: any[];
exportColumns: any[];
ngOnInit() {
this
.tutorials = [
{
title:
'Queue'
,
category:
'Data Structure'
,
rating: 4
},
{
title:
'Circularly LinkedList'
,
category:
'Data Structure'
,
rating: 5
},
{
title:
'Doubly LinkedList'
,
category:
'Data Structure'
,
rating: 3
},
{
title:
'Singly LinkedList'
,
category:
'Data Structure'
,
rating: 4
},
{
title:
'Doubly Ended Queue'
,
category:
'Data Structure'
,
rating: 5
},
{
title:
'Binary Search Tree'
,
category:
'Data Structure'
,
rating: 4
},
{
title:
'Red Black Tree'
,
category:
'Data Structure'
,
rating: 5
},
{
title:
'Breadth First Search'
,
category:
'Graph'
,
rating: 4
},
{
title:
"Floyd's Cycle"
,
category: 'Algorithm
',
rating: 4
},
{
title: '
Travelling Salesman Problem
',
category: '
Algorithm
',
rating: 5,
},
{
title: '
Bellman Ford
',
category: '
Graph
',
rating: 4
},
{
title: '
KMP Algorithm
',
category: '
String
',
rating: 5
},
];
}
exportExcel() {
import('
xlsx
').then((xlsx) => {
const worksheet = xlsx.utils.json_to_sheet(this.tutorials);
const workbook = {
Sheets: { data: worksheet },
SheetNames: ['
data
']
};
const excelBuffer: any = xlsx.write(workbook, {
bookType: '
xlsx
',
type: '
array
',
});
this.saveAsExcelFile(excelBuffer, '
tutorials
');
});
}
saveAsExcelFile(buffer: any, fileName: string): void {
let EXCEL_TYPE =
'
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8
';
let EXCEL_EXTENSION = '
.xlsx
';
const data: Blob = new Blob([buffer], {
type: EXCEL_TYPE,
});
FileSaver.saveAs(
data,
fileName + '
_export_' +
new
Date().getTime() + EXCEL_EXTENSION
);
}
}
export interface Tutorial {
title?: string;
category?: string;
rating?: number;
}