1 <div style="margin-bottom:5px" id="tb">
 2 <a href="#" class="easyui-linkbutton" onclick="return Save_Excel()" iconCls="icon-save" plain="true" title="导出excel文件"></a>
 3 </div>
 4 <table id="dg"></table>
 5 <script>
 6         function Save_Excel() {//导出Excel文件
 7             //getExcelXML有一个JSON对象的配置,配置项看了下只有title配置,为excel文档的标题
 8             var data = $(\'#dg\').datagrid(\'getExcelXml\', { title: \'datagrid import to excel\' }); //获取datagrid数据对应的excel需要的xml格式的内容
 9             //用ajax发动到动态页动态写入xls文件中
10             var url = \'datagrid-to-excel.ashx\'; //如果为asp注意修改后缀
11             $.ajax({ url: url, data: { data: data }, type: \'POST\', dataType: \'text\',
12                 success: function (fn) {
13                     alert(\'导出excel成功!\');
14                     window.location = fn; //执行下载操作
15                 },
16                 error: function (xhr) {
17                     alert(\'动态页有问题\nstatus:\' + xhr.status + \'\nresponseText:\' + xhr.responseText)
18                 }
19             });
20             return false;
21         }
22         $(function () {
23             $(\'#dg\').datagrid({
24                 singleSelect: true,
25                 toolbar:\'#tb\',
26                 url: \'product.json\', 
27                 fitColumns: true, pagination: true, pageSize: 3,
28                 title: \'easyui datagrid数据导出excel文件示例\',
29                 width: 400,
30                 height: 300,
31                 columns: [[{ field: \'itemid\', width: 80, title: \'Item ID\' },
32                  { field: \'productname\', width: 100, editor: \'text\', title: \'Product Name\' },
33                  { field: \'listprice\', width: 80, align: \'right\', title: \'List Pirce\' },
34                  { field: \'unitcost\', width: 80, align: \'right\', title: \'Unit Cost\'}]]
35             });
36         });
37     </script>

 2.

new Ext.Button({
    text: \'导出到Excel\',
    handle: function() {
        var vExportContent = grid.getExcelXml();
        if (Ext.isIE6 || Ext.isIE7 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) {
            if (! Ext.fly(\'frmDummy\')) {
                var frm = document.createElement(\'form\');
                frm.id = \'frmDummy\';
                frm.name = id;
                frm.className = \'x-hidden\';
                document.body.appendChild(frm);
            }
            Ext.Ajax.request({
                url: \'/exportexcel.php\',
                method: \'POST\',
                form: Ext.fly(\'frmDummy\'),
                callback: function(o, s, r) {
                    //alert(r.responseText);
                },
                isUpload: true,
                params: {exportContent: vExportContent}
            })
        } else {
            document.location = \'data:application/vnd.ms-excel;base64,\' + Base64.encode(vExportContent);
        }
});

 

版权声明:本文为jasonlai2016原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/jasonlai2016/p/9773203.html