2017-07-11 9 views
1

htmlテーブルのデータをExcelファイルにエクスポートしようとしていますが、IEとFirefoxではダウンロード機能が動作しません。 Chromeでは問題なく動作します。IEとFirefoxでtableToExcelが機能しない

サービス工場 -

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 - ExcelシートにHTMLテーブルをエクスポート中

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

答えて

0

は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 
}; 
関連する問題