Unicode UTF-8 Dingbats 坏了,但太多了

Unicode UTF-8 Dingbats are broken when a few but fine too many

您好,我正在尝试将 html table 转换为 excel。一切都很好,除了 excel,当有 20 个或更多时,dingbats 很好,但当有更少时就坏了..

这是我的 javascript :

 $("#btnExport").click(function (e) {
    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]; }) }


    table = document.getElementById('table')
    var ctx = { worksheet: 'Table1' , table: table.innerHTML }

    var a = document.createElement('a');
    a.href = uri + base64(format(template, ctx))

    a.download = 'Excel.xls';
    a.click();
    e.preventDefault();
});

截图:

工作正常:

损坏:

dingbats 的值:(你可以从 here 看到它)

&#x2714;->✔ &#x2718->✘

确定这完全可以工作,而无需在某处附加创建的 A 元素?

但是,为了支持 UFT-8,您应该将 <meta charset="UTF-8"> 添加到模板的 head 中。

 $("#btnExport").click(function (e) {
    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> <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]--></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]; }) }


    table = document.getElementById('table')
    var ctx = { worksheet: 'Table1' , table: table.innerHTML }

    var a = document.createElement('a');
    a.href = uri + base64(format(template, ctx))

    a.download = 'Excel.xls';

    $("body").append(a);

    a.click();
    e.preventDefault();
});

参见fiddle:https://jsfiddle.net/r5p6rmtb/