Extjs 从grid中导出Excel表格。后台为C#(绝对好用)(按照自己的需求修改版本)...

最近生成Excel表格,稍微得到一点新的体会,特此更新自己开发时候的版本。

开发工具:VS2005

数据库:oracle10.1

浏览器:firefox3.5.9

 

第一步,web层aspx文件 需要载入附件中的ExportGridToExcel.js(注释的地方可以改变表Excel表格最后的展示样式)。

载入方法:在<head></head>之间写入代码:

 <!--导出excel表格脚本-->		
    <script type="text/javascript" src="../../../Js/Common/ExportGridToExcel.js"></script>

 

第二步,基本参数初始化,准备调用导出Excel表格函数ExportExcel(componentsGridPanel, config,"GetComponentsBillList");(这个方法大家可以自己定义参数)

 //==导出Excel配置==//
    var config = {
        store: null,//因为后续可能需要处理分页,因此此处一般不直接传递GridPanel的数据源
     title: '',//标题
     checkId: '',
        storageName: '',
        beginTime: '',
        endTime: '',
        checker: ''
    };
    
    //盘点单基本信息设置
    config.title = title;
    config.checkId = checkId;
    config.storageName = storageName;
    config.beginTime = beginTime;
    config.endTime = endTime;
    config.checker = checker;
    
    //导出Excel表格,生成盘点计划中的部件盘点单  公共函数 CheckWarehouseView.js文件中
    //属性解释:gridPanel名,基本配置信息,store后台方法
    ExportExcel(componentsGridPanel, config,"GetComponentsBillList");

 第三步,函数ExportExcel(gridPanel, config,operate)//原先有部分浏览器的判断,导致导出的文件中文名无法传递。去掉后,到目前没出现安全问题。

//==导出Execl表格==//
//gridPanel:当前grid,config:生成单配置,operate:后台url调用的方法
function ExportExcel(gridPanel, config,operate){
    if (gridPanel) {
        var tmpStore = gridPanel.getStore();
        var tmpExportContent = '';        
        var tmpAllStore = new Ext.data.GroupingStore({//重新定义一个数据源
            proxy: tmpStore.proxy,
            reader: tmpStore.reader
        });
        
        tmpAllStore.load({//获取所有数据
            params: {
                secondLevelStorageId: secondLevelStorageId,
                thirdLevelStorageId: thirdLevelStorageId,
                operate: operate
            }
        });
        tmpAllStore.on('load', function(store){
            config.store = store;
            tmpExportContent = gridPanel.getExcelXml(false, config);//此方法用到了一中的扩展
                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: '../../../Url/Common/ExportServicePage.aspx',//将生成的xml发送到服务器端
                    method: 'POST',
                    form: Ext.fly('frmDummy'),
                    callback: function(o, s, r){
                        //alert(r.responseText);                    
                    },
                    isUpload: true,
                    params: {
                        ExportContent: tmpExportContent,
                        ExportFile: config.storageName+"-"+config.title+ '.xls'
                    }
                });
        });
    }
};

 

第四步,后台页面ExportServicePage.aspx代码(去掉编码处理,这样可以避免传到客户端的文件为乱码)

 

 

//===============================================================================
// Copyright (C) 2010 XXXX有限公司。版权所有。 
//===============================================================================
// 
// 文件名:ExportServicePage.aspx.cs
//
// 文件描述:导出Excel表格
//
//
// 创建人:truman
// 创建时间:2010年3月24日
//
// 修改人:
// 修改时间:
// 修改描述:
//===============================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AfcMaintenanceSystem.Url.Common
{
    public partial class ExportServicePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["ExportContent"] != "")
                {

                    string tmpFileName = "export.xls";
                    string tmpContent = Request["ExportContent"];//获取传递上来的文件内容
                    if (Request["ExportFile"] != "")
                    {
                        tmpFileName = Request["ExportFile"];//获取传递上来的文件名
                        //tmpFileName = System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(tmpFileName));//处理中文文件名的情况 
                    }

                    Response.Write("&amp;lt;script&amp;gt;document.close();&amp;lt;/script&amp;gt;");
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = "application/vnd.ms-excel";
                    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + tmpFileName + "\"");
                    Response.Charset = "";

                    this.EnableViewState = false;
                    System.IO.StringWriter tmpSW = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter tmpHTW = new System.Web.UI.HtmlTextWriter(tmpSW);
                    tmpHTW.WriteLine(tmpContent);
                    Response.Write(tmpSW.ToString());
                    Response.End();

                }
            }
        }
    }
}

 

 在界面成gridPanel中设计好布局的格式,经过如上几步代码,即可下载到自己想要格式的Excel文件。

web界面展示:

 

Excel表格展示:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用ExtJS 4.2.1的完整例子,可以将Grid表格导出Excel文件: ``` Ext.require([ 'Ext.grid.*', 'Ext.data.*', 'Ext.util.*', 'Ext.toolbar.Paging', 'Ext.ux.exporter.Exporter' ]); Ext.onReady(function() { // 模拟数据 var data = []; for (var i = 0; i < 20; i++) { data.push({ name: 'User ' + i, email: 'user' + i + '@example.com', phone: '555-' + Ext.Number.randomInt(1000, 9999) }); } // 创建数据模型 Ext.define('User', { extend: 'Ext.data.Model', fields: ['name', 'email', 'phone'] }); // 创建数据源 var store = Ext.create('Ext.data.Store', { model: 'User', data: data, proxy: { type: 'memory', reader: { type: 'json', root: 'data' } } }); // 创建Grid表格 var grid = Ext.create('Ext.grid.Panel', { store: store, columns: [{ text: 'Name', dataIndex: 'name' }, { text: 'Email', dataIndex: 'email', flex: 1 }, { text: 'Phone', dataIndex: 'phone' }], width: 400, height: 200, dockedItems: [{ xtype: 'toolbar', dock: 'top', items: [{ xtype: 'button', text: 'Export to Excel', handler: function() { // 导出Excel Ext.ux.exporter.Exporter.exportGrid(grid, 'Excel', { title: 'User List' }); } }] }, { xtype: 'pagingtoolbar', store: store, dock: 'bottom', displayInfo: true }] }); // 渲染Grid表格 Ext.create('Ext.container.Viewport', { layout: 'fit', items: [grid] }); }); ``` 在上面的代码,我们首先定义了一个模拟数据数组,然后创建了一个数据模型和一个数据源。接下来,我们创建了一个Grid表格,将数据源绑定到该表格,并添加了一个工具栏和一个分页工具栏。在工具栏,我们添加了一个按钮,当用户单击该按钮时,将调用Ext.ux.exporter.Exporter.exportGrid函数将Grid表格导出Excel文件。 需要注意的是,我们使用了Ext.ux.exporter.Exporter类来执行导出操作。该类是ExtJS的一个扩展,需要在应用程序明确声明。在本例,我们使用了Ext.require函数来加载必需的类和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值