Tu, utilizzi tabelle complete?
ad esempio
Codice PHP:
Es Table
<table id="table">
<thead>
<tr>
<th>Campo1</th>
<th>Campo2</th>
<th>Campo3</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
<td>13</td>
</tr>
<tr>
<td>21</td>
<td>22</td>
<td>23</td>
</tr>
<tr>
<td>31</td>
<td>32</td>
<td>33</td>
</tr>
<tr>
<td>41</td>
<td>42</td>
<td>43</td>
</tr>
</tbody>
</table>
In questo caso ottieni l'elemento Table
Codice PHP:
var tab = document.getElementById('table'); //Valido
var csvStr = "";
var c, htr, h;
for(c = tab.firstChild; c != null; c = c.nextSibling)
{
if(c.nodeType == 1)
{
if(c.nodeName.toLowerCase() == 'thead')
{
for(htr = c.firstChild; htr != null; htr = htr.nextSibling)
{
if(htr.nodeType==1)
{
for(h = htr.firstChild; h != null; h = h.nextSibling)
{
if(h.nodeType == 1)
{
csvStr += "'"+h.innerHTML+"',";
}
}
}
}
csvStr = csvStr.substring(0, csvStr.length - 1);
csvStr += '\n';
}
}
}
for(c = tab.firstChild; c != null; c = c.nextSibling)
{
if(c.nodeType == 1)
{
if(c.nodeName.toLowerCase() == 'tbody')
{
for(htr = c.firstChild; htr != null; htr = htr.nextSibling)
{
if(htr.nodeType==1)
{
for(h = htr.firstChild; h != null; h = h.nextSibling)
{
if(h.nodeType == 1)
{
csvStr += "'"+h.innerHTML+"',";
}
}
csvStr = csvStr.substring(0, csvStr.length - 1);
csvStr += '\n';
}
}
}
}
}
alert(csvStr);
Dovrebbe andare bene, ma la tabella deve essere formata con il thead ed il tbody per assicurarsi di distinguere head e body del CVS