0

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>
Kinjal Parmar
  • 342
  • 1
  • 3
  • 17
  • https://support.microsoft.com/en-us/help/948615/error-opening-file-the-file-format-differs-from-the-format-that-the-fi The warning is a result of "Extension Hardening" - basically the file content format doesn't match what's expected based on the extension. That's to be expected since HTML is not an Excel file format. – Tim Williams Aug 22 '18 at 06:16
  • @TimWilliams Yeah I know that but I have no idea how to change HTML format to Excel format. Can you please help? – Kinjal Parmar Aug 22 '18 at 06:26
  • https://stackoverflow.com/questions/37498713/how-to-export-an-html-table-as-an-xlsx-file – Tim Williams Aug 22 '18 at 06:29
  • @TimWilliams That link is suggesting to send table data to server but I want to do it without sending to server. – Kinjal Parmar Aug 22 '18 at 10:41

1 Answers1

-1

Use extension xlsx instated of xls , Please try and let me know. we did resolved error like this.

sa = txtArea1.document.execCommand("SaveAs", true, "MySheet.xlsx");
Abiuth Arun
  • 169
  • 1
  • 4