1 <button type="button" id="export" onclick="exportTableToExcel('tblData')">Export List</button>
2
3<script>
4 function exportTableToExcel(tableID, filename = '') {
5 var downloadLink;
6 var dataType = 'application/vnd.ms-excel';
7 var tableSelect = document.getElementById(tableID);
8 var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');
9
10 // Specify file name
11 filename = filename ? filename + '.xls' : 'excel_data.xls';
12
13 // Create download link element
14 downloadLink = document.createElement("a");
15
16 document.body.appendChild(downloadLink);
17
18 if (navigator.msSaveOrOpenBlob) {
19 var blob = new Blob(['\ufeff', tableHTML], {
20 type: dataType
21 });
22 navigator.msSaveOrOpenBlob(blob, filename);
23 } else {
24 // Create a link to the file
25 downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
26 // Setting the file name
27 downloadLink.download = filename;
28
29 //triggering the function
30 downloadLink.click();
31 }
32 }
33</script>
1<script type="text/javascript">
2var tableToExcel = (function() {
3 var uri = 'data:application/vnd.ms-excel;base64,'
4 , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
5 , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
6 , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
7 return function(table, name) {
8 if (!table.nodeType) table = document.getElementById(table)
9 var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
10 window.location.href = uri + base64(format(template, ctx))
11 }
12})()
13</script>
1$( document ).ready(function() {
2 $(".export").click(function() {
3 var export_type = $(this).data('export-type');
4 $('#data_table').tableExport({
5 type : export_type,
6 escape : 'false',
7 ignoreColumn: []
8 });
9 });
10});