1

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.

4
  • are you using angular cli ? take a look at the documentation github.com/angular/angular-cli/wiki/stories-third-party-lib Commented Sep 21, 2017 at 10:45
  • Can you share your code where you have tried the above plugin? If you haven't are you asking how to import ? because here is a documentation for the same. npmjs.com/package/json2csv Commented Sep 22, 2017 at 12:18
  • @CliveMac, I am trying to find a solution for Angular 4 Commented Sep 25, 2017 at 5:36
  • @Suvonkar if that is the case then, I believe "nikhilbaby" has provided the correct answer below. Commented Sep 25, 2017 at 12:04

3 Answers 3

9

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Are you using Angular 5?. This doesn't seem to work with Angular 5. There is an open issue for the same in: github.com/aqeel-legalinc/angular2-json2csv/issues/11
You should try the solution provided by @Diego. I believe that works.
4
npm install --save angular2-csv

For Angular [ 2,4,5 ] install old version:

npm install --save [email protected]

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) });

Source

4 Comments

This does not work anymore too. The library recently updated so just refer to the guide on Github.
@JuniBrosas npm install --save angular2-csv For Angular [ 2,4,5 ] install old version: npm install --save [email protected]
It generates csv file without headers (column names). Any idea?
@AnkurAkvaliya set header this mode => const myArrStr = JSON.stringify(this.someArray); var head = ['header 1', 'header 2', 'header 3']; new Angular2Csv(myArrStr, 'report_name', { headers: (head) });
2
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.

1 Comment

how about nested objects? Example {"column4": "Column 4", "city": { "id": 125, "name": "SomeCity" } ... Is posible to show the name of the city??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.