2
How do I export an html table to a CSV file?
In my code below I am saving the csv file, but when opening in excel does not present the column divisions.
I would like to export directly to CSV. But I can’t find any way to do that for Chrome and IE.
Follows 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.
do you want a solution that uses JAVASCRIPT ONLY? a solution where Javascript requests a server side component so that it generates its CSV file and makes it available for download would solve its problem?
– Marcelo Bezerra bovino
Excel does properly open the columns of a CSV provided that you select the correct opening options and that the file is valid. Have you checked these two situations?
– Vinícius Gobbo A. de Oliveira
Marcelo, I am open to any type of solution that exports my html table to a csv file. Previously I had done in java a function that generates the csv file, but I could not download it.
– Douglas Emerick
Vinícius, when saving the IE file gives only options to save to HTML or TXT. I am saving the file with the CSV extension of type TXT or HTML. When I open the file excel does not separate the columns in cells.
– Douglas Emerick
I’m posting again a code that worked for Chrome. In Chrome when I download CVS opens correctly in excel. But in IE when I download it does not divide the columns by ";". The file in excel gets disfigured.
– Douglas Emerick