Skip to content

convert array to csv file, and allow browser to automatically download the file

License

Notifications You must be signed in to change notification settings

Cody2333/csv-exportor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

csv-exportor

A Javascript library for browsers to generate and download csv file from array.

The csv parser is based on module comma-separated-values

refer to comma-separated-values to see all the allowed array patten and options.

using window.URL.createObjectURL to create file stream, so please pay attention to the browser compatibility.

Demo

demo page

demo source code

/*
using vue2 to demo its usage
by click the button, 
your brower would start a download 
of file 'test.csv' immediately.
*/
<template>
	...
	<button @click="exportCsv">EXPORT</button>
	...
</template>


import CsvExportor from 'csv-exportor'

export default {
  data() {
    return {
      tableData: [["a","b","c"],["d","e","f"]],
      header: ["foo","bar","baz"],
    }
  },
  methods: {
    exportCsv() {
      CsvExportor.downloadCsv(
        tableData, 
        { header }, 
        'test.csv');
    }
  }

Installation

Use Yarn or npm:

$ yarn add csv-exportor
$ npm i csv-exportor --save

Module Loading

It's an UMD module, so you can:

// ECMAScript 2015+ (via Babel)
import CsvExportor from 'csv-exportor'
// CommonJS
const CsvExportor = require('csv-exportor')
<!-- browser globals -->
<script src="YOUR_PATH_TO/node-modules/csv-exportor/dist/csv-exportor.min.js"></script>

Usage

import CsvExportor from 'csv-exportor'

/*
 generate csv file from data,
 and automaticly download the file for browser
 */
CsvExportor.downloadCsv(data, options, fileName);

// generate data url of the generated csv file
CsvExportor.genUrl(data, options);

LICENSE MIT

About

convert array to csv file, and allow browser to automatically download the file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published