Skip to main content
@spa-tools/utilities

downloadCsvFile()

The downloadCsvFile() does just what it sounds like: it takes CSV data and downloads it as a file from within the user's browser. This function is super useful when you want to provide a quick and easy CSV export feature from you SPA (e.g. from a data table being viewed).

Usage

import { downloadCsvFile } from '@spa-tools/utilities';

const data = [
{ name: 'John Doe', age: 30, city: 'New York' },
{ name: 'Jane Doe', age: 25, city: 'Los Angeles' },
{ name: 'Jim Doe', age: 35, city: 'Chicago' },
];

downloadCsvFile(data, 'people.csv', ['Name', 'Age', 'City']);

Arguments

NameTypeRequired?DefaultDescription
csvDatastring[]Yes-The data to be exported to CSV
fileNamestringYes-The name to be used for the downloaded file
columnNamesstring[]No-The column names (i.e. header) to be used as first row in the CSV file. If omitted, no header will be included.