There’s nothing that makes me want to thrash a bureaucrat more than the HTML-table-to-Excel request. But if you’ve gotta…
Turn table into CSV
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Send in a unique identifier for a table, like #myStupidTable exportdefaultfunctiontable2csv(theTable){ let rows = document.querySelectorAll(`${theTable} tr`); let csvData = "";
for (let i = 0; i < rows.length; i++) { let cells = rows[i].querySelectorAll('th, td'); let dataArray = []; for (let n = 0; n < cells.length; n++) { dataArray.push(cells[n].innerText); } csvData += dataArray.map(val =>`"${val.replace(/"/g, '')}"`).join(",") + '\n'; }
return csvData; }
Next, suppose you have a download button like this. Note the download attribute - we can give this sucker a useful name rather than ‘download’ with no extension.