Javascript 导出 table 到 excel UTF-8 问题

Javascript export table to excel UTF-8 problem

我导出到 excel 的效果非常好,只是没有 UTF-8 字符(示例:ÄÖÜÕ)

我使用的代码:

function fnExcelReport()
{
    var tab_text="<table border='2px'><tr bgcolor='#c1292e'>";

    var textRange; var j=0;
    tab = document.getElementById('user_data'); // id of table

    for(j = 0 ; j < tab.rows.length ; j++) 
    {     
        tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
        //tab_text=tab_text+"</tr>";
    }

    tab_text=tab_text+"</table>";
    tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if u want links in your table
    tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if u want images in your table


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

    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./))      // If Internet Explorer
    {
        txtArea1.document.open("txt/html","replace");
        txtArea1.document.write(tab_text);
        txtArea1.document.close();
        txtArea1.focus(); 
        sa=txtArea1.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
    }  
    else                 //other browser not tested on IE 11
        sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

    return (sa);
}

我尝试将编码添加到 excel 导出,但遗憾的是没有成功。

tab_text = tab_text + '<head><meta charset="UTF-8" /><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>';

我在标签上也有编码。

有什么建议吗? :).

不幸的是,escape is almost deprecated,但这是您更快的解决方案: 替换

   sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));  

   sa = window.open('data:application/vnd.ms-excel,' + escape(tab_text));  

会成功的。

好像是excel和encodeURIComponent的问题 Does VBA have any built in URL decoding?。您可以在这里找到其他解决方案