I need some help to export a JSON to CSV in Angular 4.
Is there any ready made external plugin to serve my requirement.
I have found one plugin json2csv. But I have no idea how to use it in Angular 4.
I need some help to export a JSON to CSV in Angular 4.
Is there any ready made external plugin to serve my requirement.
I have found one plugin json2csv. But I have no idea how to use it in Angular 4.
You can use the angular 2 version of the library. The link to the same is: https://github.com/aqeel-legalinc/angular2-json2csv
Basic Usage:
Data
is the JSON object that has to be converted to CSV. Filename
is the name of the output file. The steps included are:
Install the library
npm install angular2-json2csv --save
Import the package in the component
import {CsvService} from 'angular2-json2csv'
Add the service in the constructor of the component
constructor(private csvService: CsvService) {
}
Call the service
this.csvService.download(this.Data, 'Filename');
npm install --save angular2-csv
For Angular [ 2,4,5 ] install old version:
npm install --save angular2-csv@0.2.5
In component.ts
import { Angular2Csv } from 'angular2-csv/Angular2-csv';
var data = [
{
name: "Test 1",
age: 13,
average: 8.2,
approved: true,
description: "using 'Content here, content here' "
},
];
new Angular2Csv(data, 'My Report');
For set headers try it =>
var head = ['name', 'age', 'average', 'etc'];
new Angular2Csv(data, 'My Report', { headers: (head) });
npm install "angular2-csv": "^0.2.5",
import { Angular2Csv } from 'angular2-csv';
const sample_CSV_data = {
"column1": "Column 1",
"type": "Type",
"Board": "Board",
"column4": "Column 4"
};
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalseparator: '.',
showLabels: true
};
const csv = new Angular2Csv(sample_CSV_data, 'Sample_filename', options);
This Works fine, very helpful tool.