File Format and Extension of Don’t Match in Excel JS

Viewed 399

I have this code where I download an Excel file in .xls, however once the file is downloaded I always get the same warning:

The File Format and Extension of Don’t Match’...

The code I have is this:

function ExportExcel(table,button,name){    
//mostramos
var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><meta http-equiv="Content-type" content="application/vnd.ms-excel;charset=utf-8" />';
//tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><meta http-equiv="Content-type" content="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8" />';

tab_text = tab_text + '<x:Name>List Sites</x:Name>';

tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';

tab_text = tab_text + "<table border='1px'>";
tab_text = tab_text + $('#'+table).html();
tab_text = tab_text + '</table></body></html>';

var data_type = 'data:application/vnd.ms-excel';
//var data_type = 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
//var data_type = 'Content-Type: data:application/vnd.ms-excel'

var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");

if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {

    if (window.navigator.msSaveBlob) {
        var blob = new Blob([tab_text], {
            type: "application/csv;charset=utf-8;"
        });
        navigator.msSaveBlob(blob, name+'.xls');
    }
} else {
    $('#'+button).removeAttr("onclick");
    $('#'+button).attr('href', data_type + ', ' + encodeURIComponent(tab_text));
    $('#'+button).attr('download', name+'.xls');
    $('#'+button)[0].click();
    $('#'+button).attr('href', '#');
    $('#'+button).removeAttr("download");
    $('#'+button).attr("onclick","javascript:ReadyToExport('TablaPBN','clickdonwload');");
} }

I have already tried everything, from changing the extension to .xlsx, changing the data application (if you look at the commented codes), etc. and I can't find the problem, could someone help me?

Greetings!

0 Answers
Related