export-to-excel.js
v2.0.5
Published
An easy to use package to export your data into nice xls file.
Downloads
188
Readme
Excel Export Service Package
An easy to use package to export your data into nice xls file. Be aware that this package send axios requests to an external server so you must be connected to the net, and it might take few seconds.
Install
npm install export-to-excel.js
Usage
First of all you need to wake up the external server, like this:
import ExcelService from 'export-to-excel.js';
ExcelService.wakeUp();
Put this code immediately after your application is up (it takes faw seconds to wake up the external server).
Simple export to excel
import ExcelService from 'export-to-excel.js';
const data = [{ "id": 5, "name": "John", "grade": 90, "age": 15 }, { "id": 7, "name": "Nick", "grade": 70, "age": 17 }];
ExcelService.export(data);
The excel file that will be downloaded looks like -
Excel with headings If you want to control the excel headings you can do it easily by sending list of headings to the function:
import ExcelService from 'export-to-excel.js';
const data = [{ "id": 5, "name": "John", "grade": 90, "age": 15 }, { "id": 7, "name": "Nick", "grade": 70, "age": 17 }];
const headings = ["Student ID", "Student Name", "Test Grade", "Student Age"];
ExcelService.export(data, { headings });
The excel file that will be downloaded looks like -
Name the file Currently the excel file name is 'excel'. You can override it:
const data = [{ "id": 5, "name": "John", "grade": 90, "age": 15 }, { "id": 7, "name": "Nick", "grade": 70, "age": 17 }];
const headings = ["Student ID", "Student Name", "Test Grade", "Student Age"];
const fileName = 'StudentsGrades';
ExcelService.export(data, { headings, fileName });
Async/Await
The method 'export' is promise based, so you can use it with async/await syntax:
const onExport = async () => {
await ExcelService.export(this.data, { fileName: this.excelName });
alert('Your excel file has been exported!'); // or some nicer ui indication...
}
Options
| option | description | default value | | ------ | ------ | ------ | | fileName | the name of the excel file to be exported | excel | | headings | the headings for the excel file, if not presented the headings will be the object keys | - |