Quantcast
Channel: Internet Explorer Web Development forum
Viewing all articles
Browse latest Browse all 3527

How to export multiple html table contents into excel multiple sheets - Using Javascript

$
0
0

I am trying to export multiple hidden tables from a webpage to an Excel workbook with one worksheet per table.

Currently I am using the below html tables (['tbl1','tbl2']) and javascript function, code works to generate excelsheet but only the first table content is being exported instead exporting 'tbl1' data into sheet1 and 'tbl2' data into sheet2.

<table id="tbl1">
                <thead>
                    <tr hidden="" style="height:100px"> <th colspan="8"></th></tr>
                    <tr><td colspan="3" style="text-align:center" >Test 1</td></tr>
                </thead>
                <tbody>
                    <tr><td>Test 1</td></tr>
                </tbody>
                <tfoot>
                    <tr><td colspan="8" style="background-color:#014f7c; align-content:center; vertical-align:middle;"> Test 1 </td></tr>
                </tfoot>
            </table>
            
<table id="tbl2">
                <thead>
                    <tr hidden="" style="height:100px"> <th colspan="8"></th></tr>
                    <tr><td colspan="3" style="text-align:center" >Test 2</td></tr>
                </thead>
                <tbody>
                    <tr><td>Test 2</td></tr>
                </tbody>
                <tfoot>
                    <tr><td colspan="8" style="background-color:#014f7c; align-content:center; vertical-align:middle;"> Test 2 </td></tr>
                </tfoot>
            </table>    

<button onclick="myFunction(['tbl1','tbl2'])">Export to Multiple Excel Sheets</button>
<script type="text/javascript">
    function myFunction(tables) {
        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]; }) };    
        
        var worksheetsData = "";        
        
        for (var i = 0; i < tables.length; i++) {
            var table = document.getElementById(tables[i]);
            
            var dataValue = table.outerHTML;
            
            ctx = { worksheet: 'sheet' + i, table: dataValue };                        
            worksheetsData += format(template, ctx);
        }

        var link = document.createElement("A");
        link.href = uri + base64(worksheetsData);
        link.download = 'ExportData' || 'Workbook.xls';
        link.target = '_blank';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
</script>


Viewing all articles
Browse latest Browse all 3527

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>