Created
June 7, 2019 22:28
-
-
Save james4388/c7a487f7e0db7745189ad2ca8e844eb2 to your computer and use it in GitHub Desktop.
Generate CSV with javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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