How do I export an html table to a CSV file?
In my code below I'm saving the csv file, but when opening in excel it does not display the column divisions.
I would like to export directly to CSV. But I can not find any way to do that for chrome and IE.
Follow my code:
'<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Export table to Excel in 10 lines</title>'
'<script>'
'function Exportar(){
var textRange; var j=0;
var tab = document.getElementById("Tabela"); // id da tabela
if(document.getElementById("Tabela").rows.length < 2){
alert("Por favor, realizar uma pesquisa antes de exportar.");
}
else{
var a=[], csv='', LF='\r\n', r, c, rs, cs, row, cell, i, j, v;
for (r=0; r<tab.rows.length; r++){
row = tab.rows[r];'
for (c=0; c<row.cells.length; c++){
cell = row.cells[c];
rs = cell.rowSpan+r;
cs = cell.colSpan+c;
for (i=r; i<rs; i++){
if (!a[i]){
a[i]=[];
}
for (j=c; j<cs; j++){
a[i].push(i>r || j>c ? '' : cell.innerHTML);
}
}
}
}
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // Se for Internet Explorer
{
for (r=0; r<a.length; r++){
v = '';
for (c=0; c<a[r].length; c++){
csv += (v + a[r][c]);
v = ';';
}
csv += '\r\n';
}
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(csv);
txtArea1.document.close();
txtArea1.focus();
sa = txtArea1.document.execCommand("SaveAs",true,"Socilitações.csv");
}
else
for (r=0; r<a.length; r++){
v = '';
for (c=0; c<a[r].length; c++){
csv += (v + a[r][c]);
v = ';';
}
csv += "%0A";
}
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + csv;
a.target = '_blank';
a.download = 'Solicitações.csv';
document.body.appendChild(a);
a.click();
//sa = window.open('data:attachment/csv,' + encodeURIComponent(csv));
return (sa);
}}
'</script>'
'</head>'
'<body>
<button onclick="Exportar()">Exportar</button>
<br/>
<span>
<table id="Tabela">
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
</table>
<iframe id="txtArea1" style="display:none"></iframe>
</span>
</body>
</html>'
Can you help me?
Thank you.