Warning when opening exported table from TableToExcel javascript.
The file you are trying to open, 'MySheet.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
Here is my javascript code:
<script type="text/javascript">
var tableToExcel = (function () {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table).outerHTML
txtArea1.document.open("txt/html", "replace");
txtArea1.document.write(table);
txtArea1.document.close();
txtArea1.focus();
sa = txtArea1.document.execCommand("SaveAs", true, "MySheet.xls");
return sa;
}
}
else {
var uri = 'data:application/vnd.ms-excel;base64,'
, 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]--></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name) {
var a = document.createElement('a');
document.body.appendChild(a);
//getting data from our div that contains the HTML table
var ctx = { worksheet: 'Worksheet', table: document.getElementById('report_table').innerHTML }
//window.location.href = uri + base64(format(template, ctx))
a.href = uri + base64(format(template, ctx));
//setting the file name
a.download = 'MySheet' + '.xls';
//triggering the function
a.click();
//just in case, prevent default behaviour
}
}
})()
</script>