I\'m learning angular2 and i wanted to use js-xlsx library in my project.
I installed xlsx npm install xlsx
and jszip npm install jszip
an
The following is a working component who exports an xlsx file from an array of objects using js-xlsx lib on Angular 2/4:
import { Component, OnInit } from '@angular/core';
import { utils, write, WorkBook } from 'xlsx';
import { saveAs } from 'file-saver';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
table = [
{
First: 'one',
Second: 'two',
Third: 'three',
Forth: 'four',
Fifth: 'five'
},
{
First: 'un',
Second: 'deux',
Third: 'trois',
Forth: 'quatre',
Fifth: 'cinq'
},
];
ngOnInit() {
const ws_name = 'SomeSheet';
const wb: WorkBook = { SheetNames: [], Sheets: {} };
const ws: any = utils.json_to_sheet(this.table);
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
const wbout = write(wb, { bookType: 'xlsx', bookSST: true, type:
'binary' });
function s2ab(s) {
const buf = new ArrayBuffer(s.length);
const view = new Uint8Array(buf);
for (let i = 0; i !== s.length; ++i) {
view[i] = s.charCodeAt(i) & 0xFF;
};
return buf;
}
saveAs(new Blob([s2ab(wbout)], { type: 'application/octet-stream' }), 'exported.xlsx');
}
}
You have to install xlsx and file-saver for this to work.
For the working example on exporting a xlsx file from an array of jsons on Angular2/4:
https://github.com/bogdancar/xlsx-json-to-xlsx-demo-Angular2