-1

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 = new ActiveXObject("Excel.Application");
            xls.visible = true;
            xls.Workbooks.Add
            for (i = 0; i < x.length; i++) {
                var y = x[i].cells;
                for (j = 0; j < y.length; j++) {
                    xls.Cells(i + 1, j + 1).Value = y[j].innerText;
                }
            }
            xls.Visible = true;
            xls.UserControl = true;
            return xls;
}

its generating Excel properly, but after generating its opening it, I have to download it after generating. I have no clue how to call window.location.href in this scenario. I have to do this in IE only.

Syed Salman Raza Zaidi
  • 2,172
  • 9
  • 42
  • 87

3 Answers3

0

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>
vineet
  • 13,832
  • 10
  • 56
  • 76
0

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;
Jim Wang
  • 11
  • 1
0
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.

Jay Raj Mishra
  • 110
  • 2
  • 9