javascript
  • excel
  • 2016-02-05 60 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表格。

    相关问题