js export excel window.location.href does not generate file name problem

A strange thing happened in the production environment today. The js code exported by excel that was written before did not have a file name, and it was all random strings before.

Code

function tableToExcelExp(str, sheetName) {
    var uri = 'data:application/vnd.ms-excel;base64,';
    var 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><meta charset="UTF-8"><!--[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]-->' +
        ' <style type="text/css">' +
        'table td {' +
        'border: 1px solid #000000;' +
        'width: 200px;' +
        'height: 30px;' +
        ' text-align: center;' +
        // 'background-color: #4f891e;' +
        // 'color: #ffffff;' +
        ' }' +
        '</style>' +
        '</head><body ><table class="excelTable">'+str+'</table></body></html>';
    var ctx = {worksheet: sheetName };
    window.location.href = uri + base64(format(template, ctx));
}

Forced to change to

function tableToExcelExp(str, sheetName,fileName) {
    var uri = 'data:application/vnd.ms-excel;base64,';
    var 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><meta charset="UTF-8"><!--[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]-->' +
        ' <style type="text/css">' +
        'table td {' +
        'border: 1px solid #000000;' +
        'width: 200px;' +
        'height: 30px;' +
        ' text-align: center;' +
        // 'background-color: #4f891e;' +
        // 'color: #ffffff;' +
        ' }' +
        '</style>' +
        '</head><body ><table class="excelTable">'+str+'</table></body></html>';
    var ctx = {worksheet: sheetName };
    var alink = $('<a style="display:none"></a>').appendTo('body');
    alink[0].href = uri + base64(format(template, ctx));
    alink[0].download = fileName;
    alink[0].click();
    alink.remove();
}

But I don’t know why it is like this. Record it. A clear friend can help.

Guess you like

Origin blog.csdn.net/qq_26276667/article/details/111473842