easyui datagrid标题和数据 导出excel

来源:互联网 发布:php添加功能的按钮 编辑:程序博客网 时间:2024/06/09 02:16
<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>easyui datagrid to excel</title>    <link rel="stylesheet" type="text/css" href="http://www.jeasyui.net/Public/js/easyui/themes/default/easyui.css">    <link rel="stylesheet" type="text/css" href="http://www.jeasyui.net/Public/js/easyui/themes/icon.css">    <link rel="stylesheet" type="text/css" href="http://www.jeasyui.net/Public/js/easyui/demo/demo.css">    <script type="text/javascript" src="http://www.jeasyui.net/Public/js/jquery.js"></script>    <script type="text/javascript" src="http://www.jeasyui.net/Public/js/easyui/jquery.easyui.min.js"></script></head><body><h2>Basic DataGrid</h2><p>easyui datagrid to excel</p><div style="margin:20px 0;"></div><table id="datag2excel" class="easyui-datagrid" title="Basic DataGrid" style="width:700px;height:250px"       data-options="singleSelect:true,collapsible:true,url:'http://www.jeasyui.net/demo/datagrid_data1.json',method:'get'">    <thead>    <tr>        <th data-options="field:'itemid',width:80">Item ID</th>        <th data-options="field:'productid',width:100">产品</th>        <th data-options="field:'listprice',width:80,align:'right'">价格</th>        <th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th>        <th data-options="field:'attr1',width:250">Attribute</th>        <th data-options="field:'status',width:60,align:'center'">Status</th>    </tr>    </thead></table><a href="#" class="easyui-linkbutton" style="" id="excelout">导出</a><script>    $(function(){        var cc = {"total":28,"rows":[            {"productid":"FI-SW-01","productname":"顺风","unitcost":10.00,"status":"P","listprice":36.50,"attr1":"加大","itemid":"EST-1"},            {"productid":"K9-DL-01","productname":"Dalmation","unitcost":12.00,"status":"P","listprice":18.50,"attr1":"Spotted Adult Female","itemid":"EST-10"},            {"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":38.50,"attr1":"Venomless","itemid":"EST-11"},            {"productid":"RP-SN-01","productname":"Rattlesnake","unitcost":12.00,"status":"P","listprice":26.50,"attr1":"Rattleless","itemid":"EST-12"},            {"selected":true,"productid":"RP-LI-02","productname":"Iguana","unitcost":12.00,"status":"P","listprice":35.50,"attr1":"Green Adult","itemid":"EST-13"},            {"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":158.50,"attr1":"Tailless","itemid":"EST-14"},            {"productid":"FL-DSH-01","productname":"Manx","unitcost":12.00,"status":"P","listprice":83.50,"attr1":"With tail","itemid":"EST-15"},            {"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":23.50,"attr1":"Adult Female","itemid":"EST-16"},            {"productid":"FL-DLH-02","productname":"Persian","unitcost":12.00,"status":"P","listprice":89.50,"attr1":"Adult Male","itemid":"EST-17"},            {"productid":"AV-CB-01","productname":"Amazon Parrot","unitcost":92.00,"status":"P","listprice":63.50,"attr1":"Adult Male","itemid":"EST-18"}        ]};        $("#datag2excel").datagrid("loadData",cc);//这行代码要放在 $(function(){}) 中,否则Uncaught TypeError: Cannot read property 'options' of undefined    });    $("body").on('click',"#excelout",function() {        $('#datag2excel').datagrid('toExcel',"文件");    });    $.extend($.fn.datagrid.methods, {        toExcel: function(jq, filename){            return jq.each(function(){                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]; }) }                var alink = $('<a style="display:none"></a>').appendTo('body');                var view = $(this).datagrid('getPanel').find('div.datagrid-view');                var table = view.find('div.datagrid-view2 table.datagrid-btable').clone();                var tbody = table.find('>tbody');                view.find('div.datagrid-view1 table.datagrid-btable>tbody>tr').each(function(index){                    $(this).clone().children().prependTo(tbody.children('tr:eq('+index+')'));                });                var head = view.find('div.datagrid-view2 table.datagrid-htable').clone();                var hbody = head.find('>tbody');                view.find('div.datagrid-view1 table.datagrid-htable>tbody>tr').each(function(index){                    $(this).clone().children().prependTo(hbody.children('tr:eq('+index+')'));                });                hbody.prependTo(table);                var ctx = { worksheet: name || 'Worksheet', table: table.html()||'' };                alink[0].href = uri + base64(format(template, ctx));                alink[0].download = filename;                alink[0].click();                alink.remove();            })        }    });</script></body></html>


效果: