Created
August 1, 2019 10:18
-
-
Save ramons03/71f97a980e61dbbd5d7cc4a9cb223e1a to your computer and use it in GitHub Desktop.
Export to csv from 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 downloadFile(fileName, urlData) { | |
console.log('downloadFile', fileName); | |
var aLink = document.createElement('a'); | |
var evt = document.createEvent("HTMLEvents"); | |
evt.initEvent("click"); | |
aLink.download = fileName; | |
aLink.href = urlData; | |
aLink.dispatchEvent(evt); | |
} | |
if (selections.length > 0) { | |
var A = [['Codigo', 'Descripcion']]; | |
for (var j = 1; j < selections.length + 1; ++j) { | |
A.push(['1234', 'DESC' ]); | |
} | |
var csvRows = []; | |
for (var i = 0, l = A.length; i < l; ++i) { | |
csvRows.push(A[i].join(';')); | |
} | |
//var csvString = csvRows.join("%0A"); | |
//var csvString = "SEP=, \n" + csvRows.join("\r\n"); | |
var csvString = csvRows.join("\r\n"); | |
var a = document.createElement('a'); | |
a.href = 'data:attachment/csv,' + encodeURIComponent(csvString); | |
a.target = '_blank'; | |
a.download = 'filename.csv'; | |
a.click(); | |
} else { | |
alert('No se eligio nada.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment