Skip to content

Instantly share code, notes, and snippets.

@james4388
Created June 7, 2019 22:28
Show Gist options
  • Save james4388/c7a487f7e0db7745189ad2ca8e844eb2 to your computer and use it in GitHub Desktop.
Save james4388/c7a487f7e0db7745189ad2ca8e844eb2 to your computer and use it in GitHub Desktop.
Generate CSV with javascript
function csvBlob(rows, export_bom) {
const BOM = '\ufeff'; // Utf-8 Bytes order mark
return new Blob([(export_bom ? BOM : '') + rows.map(function(row) {
return row.map(function(col) {
return ['"', col.replace(/"/gi, '""'), '"'].join('');
}).join(',')
}).join('\n')], {type: 'text/csv;charset=utf-8;'});
}
const blob = csvBlob(temp1);
let link = document.createElement("a");
let url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", 'data.csv');
link.innerHtml = 'Download me';
document.body.appendChild(link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment