Generate excel sheet from html tables using jquery

后端 未结 4 705
走了就别回头了
走了就别回头了 2020-12-01 15:41

I wanted to generate an excel sheet once a user clicks on a button . Basically i want to do exactly what is dicussed here

how to pass html table values to excel she

相关标签:
4条回答
  • 2020-12-01 15:56

    To answer the question about IE ( I could not answer in the comment box due to reputation restictions), the code above posted by Sharun won't work on IE 10+ ('permission denied' error). Here's a snippet that will work in IE 10+ along with older versions of IE, Chrome and FF:

    var TableToExcel = (function () {
        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 cellspacing="0" rules="rows" border="1" style="color:Black;background-color:White;border-color:#CCCCCC;border-width:1px;border-style:None;width:100%;border-collapse:collapse;font-size:9pt;text-align:center;">{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 }
            if (navigator.msSaveBlob) {
                var blob = new Blob([format(template, ctx)], { type: 'application/vnd.ms-excel', endings: 'native' });
                navigator.msSaveBlob(blob, 'export.xls')
            } else {
                window.location.href = uri + base64(format(template, ctx))
            }
        }
    })()
    
    0 讨论(0)
  • 2020-12-01 15:57

    You can use this good library: https://github.com/jmaister/excellentexport

    0 讨论(0)
  • 2020-12-01 16:01

    Here is the answer: Export dynamic html table to excel in javascript in firefox browser by insin

    var tableToExcel = (function() {
          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]; }) }
          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))
          }
        })()
    
    0 讨论(0)
  • 2020-12-01 16:09
          $("#btnExport").click(function(e) {
                  e.preventDefault();
    
                //getting data from table
                var data_type = 'data:application/vnd.ms-excel';
                var table_div = document.getElementById('table_wrapper');//table_wrapper is table id
                var table_html = table_div.outerHTML.replace(/ /g, '%20');
    
                var a = document.createElement('a');
                a.href = data_type + ', ' + table_html;
                a.download = 'table_name_' + Math.floor((Math.random() * 9999999) + 1000000) + '.xls';
                a.click();
              });   
    
    0 讨论(0)
提交回复
热议问题