I would like to know how to create a .CSV file from a HTML table. I have a Jquery code to create a .XLS file from an HTML table. But, I want to create .CSV file. Can anyone help me to figure it out?
This is the code to create a .XLS file from HTML table
function fnExcelReport() {
var tab_text = "<table border='2px'><tr bgcolor='#87AFC6'>";
var textRange; var j = 0;
tab = document.getElementById('table1'); // id of table
for (j = 0; j < tab.rows.length; j++) {
tab_text = tab_text + tab.rows[j].innerHTML + "</tr>";
//tab_text=tab_text+"</tr>";
}
tab_text = tab_text + "</table>";
tab_text = tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
tab_text = tab_text.replace(/<img[^>]*>/gi, ""); // remove if u want images in your table
tab_text = tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // reomves input params
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1.document.open("txt/html", "replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa = txtArea1.document.execCommand("SaveAs", true, "QI.xls");
}
else //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
});
Thanks.
Example of JSON file for the purpose of plunker:
{ "count": 105, "next": "sdfsdfdsfsdfsdf=2", "previous": null, "results": [ { "id": 248, "name": "kasar", "slug": "managadd", "description": "was a small child", "status": "active", "absolute_url": "http://laboursday.com", "city": { "id": 11, "name": "saaid", "slug": "deploy", "list_camera_uri": "karachi.com" }, "longitude": 234.4334, "latitude": 23.2323, "image_url": "gggadjggjasd.jpg", "image_mobile_url": "asddfggdsaf.jpg", "image_thumbnail_url": "http://masdd.com/.jpg", "image_squarethumb_url": "asdfgfer.jpg", "video_url": "swrtyhnhn.mp4", "resource_uri": "sddssdfwweff" } ] }