Skip to content
 
 

Repository files navigation

angular_logo

angular-csv | Export to CSV in Angular

npm version GitHub license Angular Node npm

A helper library for creating and downloading CSV files in Angular.

Installation

npm install --save angular-csv

Example

import { AngularCsv } from 'angular-csv/dist/Angular-csv';

const data = [
  {
    name: 'Test 1',
    age: 13,
    average: 8.2,
    approved: true,
    description: "using 'Content here, content here' "
  },
  {
    name: 'Test 2',
    age: 11,
    average: 8.2,
    approved: true,
    description: "using 'Content here, content here' "
  }
];

new AngularCsv(data, 'My Report');

API | AngularCsv(data, filename, options?)

Option Default Description
fieldSeparator , Defines the field separator character
quoteStrings " Character used to quote string fields
decimalseparator . Defines the decimal separator character. Set to "locale" to use the locale-sensitive representation
showLabels false When true, uses headers to create a header row
showTitle false When true, adds the title as the first line
title 'My Report' Title shown at the top of the file when showTitle is true
useBom true Prepends a BOM character (\ufeff) to improve Excel compatibility
useHeader false When true, only fields whose keys are listed in headers are exported
useObjHeader false When true, uses objHeader keys as export order and values as column labels
noDownload false When true, disables automatic download and returns the formatted CSV string instead
headers [] Array of column header labels (or field keys when useHeader is true)
objHeader {} Object map of data keys to custom column labels, also controls export order
nullToEmptyString false When true, converts null values to empty strings

Options Example

const options = {
  fieldSeparator: ',',
  quoteStrings: '"',
  decimalseparator: '.',
  showLabels: true,
  showTitle: true,
  title: 'Your Title',
  useBom: true,
  noDownload: false,
  headers: ['First Name', 'Last Name', 'ID'],
  useHeader: false,
  nullToEmptyString: true,
};

new AngularCsv(data, 'filename', options);

Retrieving the CSV String

Pass noDownload: true to suppress the automatic download and get the raw CSV back:

const options = { noDownload: true };
const csv = new AngularCsv(data, 'My Report', options).getCsvData();
console.log(csv);

Credits

About

Helper library for create CSV file in Angular 5

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages