Skip to content

Instantly share code, notes, and snippets.

@parth-choudhary
Last active November 30, 2017 09:32
Show Gist options
  • Save parth-choudhary/06e510f529b1c81fbbed2a80d5446e98 to your computer and use it in GitHub Desktop.
Save parth-choudhary/06e510f529b1c81fbbed2a80d5446e98 to your computer and use it in GitHub Desktop.
Download csv of all data in a HTML page inside a table
function downloadCSV(){
var csvContent = "data:text/csv;charset=utf-8,";
var table = $('.table').children().children();
var data = [];
for(i=0; i < table.length; i++)
{
var row = $(table[i]).children();
var temp = [];
for(j=0; j < row.length; j++){
temp[j] = $(row[j]).text();
}
data[i] = temp;
}
data.forEach(function(infoArray, index){
dataString = infoArray.join(",");
csvContent += index < data.length ? dataString+ "\n" : dataString;
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "data.csv");
link.click(); // This will download the data file named "data.csv".
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment