easyui datagrid to excel

 1 $.extend($.fn.datagrid.methods, {
 2     toExcel: function(jq, filename){
 3         return jq.each(function(){
 4             var uri = 'data:application/vnd.ms-excel;base64,'
 5             , 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>'
 6             , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
 7             , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
 8 
 9             var alink = $('<a style="display:none"></a>').appendTo('body');
10             var view = $(this).datagrid('getPanel').find('div.datagrid-view');
11             var table = view.find('div.datagrid-view2 table.datagrid-btable').clone();
12             var tbody = table.find('>tbody');
13             view.find('div.datagrid-view1 table.datagrid-btable>tbody>tr').each(function(index){
14                 $(this).clone().children().prependTo(tbody.children('tr:eq('+index+')'));
15             });
16             var ctx = { worksheet: name || 'Worksheet', table: table.html()||'' };
17             alink[0].href = uri + base64(format(template, ctx));
18             alink[0].download = filename;
19             alink[0].click();
20             alink.remove();
21         })
22     }
23 });

 

转载于:https://www.cnblogs.com/XiaoGer/p/4778706.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值