Last active
November 30, 2017 09:32
-
-
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
This file contains 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 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