JS HTML表格到Excel文件下载IE

时间:2015-04-13 06:28:38

标签: javascript

我有一个HTML表格,我将其内容导出到Excel,这就是我正在做的事情

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;
}

正确生成Excel,但在生成它之后,我必须在生成之后下载它。我不知道在这种情况下如何调用window.location.href。 我只能在IE中这样做。

3 个答案:

答案 0 :(得分:0)

试试这个: -

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>

答案 1 :(得分:0)

这如何。它可以在我的IE上正常工作。但是问题是它运行缓慢,将Excel完成需要花费半分钟的时间。但是还是可以的。.

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;

答案 2 :(得分: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));

“ data.d”中的Ajax回调响应 最好的方法是通过返回API响应来回调表数据。