Skip to content Skip to sidebar Skip to footer

JS HTML Table To Excel File Download IE

I have a HTML table, I am exporting its content to Excel,This is how I am doing function CreateExcelSheet() { var x = document.getElementById('testTable').rows; var xls

Solution 1:

Try this:-

HTML:-

<a  onclick="tableToExcel('table_id', 'SHEET1')" class="btn btn-primary">Export to Excel </a>

JS:-

 <script type="text/javascript">//<![CDATA[ 

        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))
            }
        })()
//]]>  
    </script>

Solution 2:

How about this.It can work from my side with IE. But the problem is it runs slow, nearly costs half a minute to get the Excel done. But it works anyway..

var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;

Solution 3:

var HeaderName = 'Download-ExcelFile';
                var ua = window.navigator.userAgent;
                var msieEdge = ua.indexOf("Edge");
                var msie = ua.indexOf("MSIE ");

                if (msieEdge > 0 || msie > 0) {
                    if (window.navigator.msSaveBlob) {
                        var dataContent = new Blob([data.d], {
                            type: "application/csv;charset=utf-8;"
                        });

                        var fileName = HeaderName + '_' + parseInt(Math.random() * 10000000000) + '.xls';
                        navigator.msSaveBlob(dataContent, fileName);
                    }
                    return;
                }

                window.open('data:application/vnd.ms-excel,' + encodeURIComponent(data.d));

Ajax callback response in "data.d" The best way is to callback your table data by returning though API response.


Post a Comment for "JS HTML Table To Excel File Download IE"