javascript
  • excel
  • html-table
  • dingbats
  • 2015-06-23 56 views 0 likes 
    0

    你好,我試圖將我的HTML表格轉換爲Excel。一切是除了在Excel細,當有20個或更多,但被破壞時有較少的裝飾符號都很好..Unicode UTF-8標誌打破幾個,但罰款太多

    這裏是我的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(); 
    }); 
    

    截圖:

    工作正常:

    enter image description here

    破碎:

    enter image description here

    的裝飾符號值:(你可以看到它從here

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

    回答

    0

    相信,這將在所有的工作不附加創建的元素某處?

    但是但是,對於支持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(); 
    }); 
    

    見琴:https://jsfiddle.net/r5p6rmtb/

    +0

    沒錯!它就是這樣兒的。 – jackjop

    相關問題