Reputation: 22969
This is a self-answered question.
How can I download an HTML <table>
as a CSV within the browser? Assuming the HTML table contains no commas in the actual data.
Here's my HTML table:
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
I don't care about older browsers, just modern browsers that fully support ES6.
Upvotes: 2
Views: 2576
Reputation: 3139
Used the answer above, but altered it for my needs. I used the following function and imported to my REACT file where I needed to download the csv file.
I had a span tag within my th files. Added comments to what most functions/methods do.
import { tableToCSV, downloadCSV } from './../Helpers/exportToCSV';
export function tableToCSV(){
let tableHeaders = Array.from(document.querySelectorAll('th'))
.map(item => {
// title = splits elem tags on '\n',
// then filter out blank "" that appears in array.
// ex ["Timestamp", "[Full time]", ""]
let title = item.innerText.split("\n").filter(str => (str !== 0)).join(" ")
return title
}).join(",")
const rows = Array.from(document.querySelectorAll('tr'))
.reduce((arr, currRow) => {
// if tr tag contains th tag.
// if null return array.
if (currRow.querySelector('th')) return arr
// concats individual cells into csv format row.
const cells = Array.from(currRow.querySelectorAll('td'))
.map(item => item.innerText)
.join(',')
return arr.concat([cells])
}, [])
return tableHeaders + '\n' + rows.join('\n')
}
export function downloadCSV(csv){
const csvFile = new Blob([csv], { type: 'text/csv' })
const downloadLink = document.createElement('a')
// sets the name for the download file
downloadLink.download = `CSV-${currentDateUSWritten()}.csv`
// sets the url to the window URL created from csv file above
downloadLink.href = window.URL.createObjectURL(csvFile)
// creates link, but does not display it.
downloadLink.style.display = 'none'
// add link to body so click function below works
document.body.appendChild(downloadLink)
downloadLink.click()
}
When user click export to csv it trigger the following function in react.
handleExport = (e) => {
e.preventDefault();
const csv = tableToCSV()
return downloadCSV(csv)
}
Example html table elems.
<table id="datatable">
<tbody>
<tr id="tableHeader" className="t-header">
<th>Timestamp
<span className="block">full time</span></th>
<th>current rate
<span className="block">alt view</span>
</th>
<th>Battery Voltage
<span className="block">current voltage
</span>
</th>
<th>Temperature 1
<span className="block">[C]</span>
</th>
<th>Temperature 2
<span className="block">[C]</span>
</th>
<th>Time & Date </th>
</tr>
</tbody>
<tbody>
{this.renderData()}
</tbody>
</table>
</div>
Upvotes: 0
Reputation: 22969
Just loop over the HTML rows tr
, joining the columns with a comma ,
and the rows using a newline /n
.
Then loop over the HTML headers th
, joining each with a comma ,
.
Then concat the header string and the row string using a newline /n
.
From then on just create a hyperlink <a>
with a set Blob
of your CSV string and force .click()
it, to start the download.
Here's an example that runs fine on Chrome 68, FF 58, MacOS Safari 11:
const tableToCSV = table => {
const headers = Array.from(table.querySelectorAll('th'))
.map(item => item.innerText).join(',')
const rows = Array.from(table.querySelectorAll('tr'))
.reduce((arr, domRow) => {
if (domRow.querySelector('th')) return arr
const cells = Array.from(domRow.querySelectorAll('td'))
.map(item => item.innerText)
.join(',')
return arr.concat([cells])
}, [])
return headers + '\n' + rows.join('\n')
}
const downloadCSV = csv => {
const csvFile = new Blob([csv], { type: 'text/csv' })
const downloadLink = document.createElement('a')
downloadLink.download = `CSV-${new Date().toDateString()}.csv`
downloadLink.href = window.URL.createObjectURL(csvFile)
downloadLink.style.display = 'none'
document.body.appendChild(downloadLink)
downloadLink.click()
}
document.querySelector('button').addEventListener('click', () => {
const table = document.querySelector('table')
const csv = tableToCSV(table)
return downloadCSV(csv)
})
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
text-align: left;
}
table#t01 {
width: 100%;
background-color: #f1f1c1;
}
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>
<button on-click="download()">Download CSV</button>
Upvotes: 2