tableToExcel在IE和Firefox中不起作用

问题描述 投票:1回答:1

我试图将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')">    
javascript html excel controller export
1个回答
0
投票

在将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
};
© www.soinside.com 2019 - 2024. All rights reserved.