2

Export HTML table to Excel

Function JavaScript 1 revisions 135 a year ago a year ago
function exportExcel(element, title) {
	var mimeType = 'data:application/vnd.ms-excel';
	var html = element.outerHTML.replace(/ /g, '%20');
	var documentPrefix = title ? title.replace(/ /g, '-') : 'Excel spreadsheet'
	var d = new Date();
	var dummy = document.createElement('a');
	dummy.href = mimeType + ', ' + html;
	dummy.download = documentPrefix
		+ '-' + d.getFullYear() + '-' + (d.getMonth()+1) + '-' + d.getDate()
		+ '-' + d.getHours() + '-' + d.getMinutes() + '-' + d.getSeconds()
		+'.xls';
	dummy.click();
}

This function takes an HTML table and downloads it as an Excel spreadsheet.

exportExcel(document.getElementById('customers'), 'Customers');