Saturday, July 8, 2017

AngularJS Excel Export

HTML:



 
   
   
   
 

 
   

Export to Excel


   
     
 Export to Excel
   
   

     

       
         
            Team
            Process Type
            Cedent
         
       
       
         
            value 1
            value 2
            value 3
         
         
            value 4
            value 5
            value 6
         
         
            10.12.2015
            AXA Affin
            101024 - Quota Share QS
         
       
     
   
 




CSS:
/* Styles go here */
.table-header 
{
background-color: lightskyblue;  
}


JAVASCRIPT:
// Code goes here
var myApp=angular.module("myApp",[]);
myApp.factory('Excel',function($window){
        var uri='data:application/vnd.ms-excel;base64,',
            template='{table}
',
            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;
            }
        };
    })
    .controller('MyCtrl',function(Excel,$timeout,$scope){
      $scope.exportToExcel=function(tableId){ // ex: '#my-table'
            var exportHref=Excel.tableToExcel(tableId,'WireWorkbenchDataExport');
            $timeout(function(){location.href=exportHref;},100); // trigger download
        }
    });

No comments:

Post a Comment

Dharamart.blogspot.in