tableToExcel在IE和Firefox中不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tableToExcel在IE和Firefox中不起作用相关的知识,希望对你有一定的参考价值。

我试图将html表数据导出到Excel文件,但下载功能在IE和Firefox中不起作用。它在Chrome中运行良好。

服务工厂 -

aixmmServices.factory('Excel',function($window){
    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 {
        tableToExcel:function(tableId,worksheetName){
            var table=$(tableId),
            ctx={worksheet:worksheetName,table:table.html()},
            href=uri+base64(format(template,ctx));
            return href;
        }
    };
});

控制器 -

frameLparReportApp.controller('frameLparReportController', function ($scope, $state, frameLparReportServices, Excel, $timeout) {
'use strict';
$("#btnExport").click(function (e) {
    window.open('data:application/vnd.ms-excel,' + $('#frameLpar').html());
    e.preventDefault();
});

$scope.exportToExcel=function(tableId){ // ex: '#my-table'
    var exportHref=Excel.tableToExcel(tableId,'frameLparReport');
    var a = document.createElement('a');
  a.href = exportHref;
$scope.currentDate = new Date().getTime().toString();
a.download = "frameLparStateListingReport_" + $scope.currentDate + ".xls";
a.click();

$timeout(function(){location.href=exportHref;},100); // trigger download
};

});

HTML -

<input type="button" id="btnExport" value="Download Frame LPAR Report"  class="btn btn-link" ng-click="exportToExcel('#frameLpar')">    
答案

在将HTML表格导出到Excel工作表时添​​加了包含IE和Firefox的功能。

 aixmmServices.factory('Excel',function($window){
    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 {
        getExportExcel: function (tableId, fileName) {

            name = fileName + '.xls';
            var table = document.querySelector(tableId),
                ctx = {table: table.innerHTML};
            var browser = window.navigator.appVersion;

            //Workaround to enable the users to download the report in IE.
            if ((browser.indexOf('Trident') !== -1 && browser.indexOf('rv:11') !== -1) ||
                (browser.indexOf('MSIE 10') !== -1)) {
                    var builder = new window.MSBlobBuilder();
                    builder.append(uri + format(template, ctx));
                    var blob = builder.getBlob('data:application/vnd.ms-excel');
                    window.navigator.msSaveOrOpenBlob(blob, name);
            } else {
                /*var link = document.createElement('a');
                link.download = name;
                link.href = uri + base64(format(template, ctx));
                link.click();  */
                var blob = new Blob([format(template, ctx)], { type: 'application/vnd.ms-excel', endings: 'native' });

                var elem = window.document.createElement('a');
                //elem.href = uri + base64(format(template, ctx));
                elem.href = window.URL.createObjectURL(blob);
                elem.download = name;
                document.body.appendChild(elem);
                elem.click();
                document.body.removeChild(elem);
            }
        }
    };
});

控制器 -

$scope.exportToExcel=function(tableId){ // ex: '#my-table'
    var d =  new Date();
    var date = d.getDate() + "" + (d.getMonth() + 1) + "" + d.getFullYear();
    var filename = "lparReport_" + date;    
    var exportHref=Excel.getExportExcel(tableId,filename);
    //    $timeout(function(){location.href=exportHref;},100); // trigger download
};

以上是关于tableToExcel在IE和Firefox中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Jquery 日期规则在 Firefox 和 IE 中不起作用

IE 和 Firefox 中不显示文本输入占位符

Font Awesome 在 Firefox 和 IE 中不起作用

Jquery DataTable TableTool 在 IE 和 Firefox 中不起作用

图标在 Chrome 和 firefox 中显示,但在 IE 中不显示

font-face 在 IE 中运行,但在 Firefox 中不运行 - 字体格式