data table js export to excel code example
Example 1: javascript save table as excel
<button type="button" id="export" onclick="exportTableToExcel('tblData')">Export List</button>
<script>
function exportTableToExcel(tableID, filename = '') {
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
filename = filename ? filename + '.xls' : 'excel_data.xls';
downloadLink = document.createElement("a");
document.body.appendChild(downloadLink);
if (navigator.msSaveOrOpenBlob) {
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob(blob, filename);
} else {
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
downloadLink.download = filename;
downloadLink.click();
}
}
</script>
Example 2: export table data to excel in jquery
$( document ).ready(function() {
$(".export").click(function() {
var export_type = $(this).data('export-type');
$('#data_table').tableExport({
type : export_type,
escape : 'false',
ignoreColumn: []
});
});
});