javascript
  • excel
  • 2016-02-05 66 views 2 likes 
    2

    我想導出HTML表到excel所以我用這個JavaScript代碼:出口大HTML表格到Excel

    var tableToExcel = (function() { 
         var tds = document.getElementsByTagName("td"); 
    
         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"><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"><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 x:str>{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]; 
          }) 
         } 
         return function (table, name) { 
          if (!table.nodeType) 
           table = document.getElementById(table); 
          var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML} 
          window.location.href = uri + base64(format(template, ctx)) 
         } 
        })(); 
    

    起初,該代碼做工精細,形成了我,但在非常大的表確實不是因爲uri對window.location.href太長。 他們有沒有辦法將大表輸出到Excel?

    +1

    參考http://stackoverflow.com/questions/19401638/export-as-xls-file-not-work-when-large-data – SaurabhM

    +0

    並可以參考這個以及http://stackoverflow.com/問題/ 22317951 /出口HTML表數據到Excel的使用,JavaScript的jQuery的是 - 不工作,properl –

    回答

    2

    我發現解決使用FileSaver.js 1這個問題的一種方法:https://github.com/eligrey/FileSaver.js/請查看以下

    HTML enter image description here

    的Javascript

    enter image description here

    這是工作正常,我與大數據集的HTML表格。

    相關問題