首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

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

2012-11-25 
Extjs 从grid中导出Excel表格。后台为C#(绝对好用)(按照自己的需求修改版本)最近生成Excel表格,稍微得到一

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界面展示:

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

?

Excel表格展示:

?

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

?

1 楼 xiefeng 2011-10-18   很好,
可是数据量大,就出问题
100条数据就出问题,怎么回事呢 2 楼 qimo601 2012-04-18   xiefeng 写道很好,
可是数据量大,就出问题
100条数据就出问题,怎么回事呢


额不好意思。。。已经两年多没用过Extjs了,完全忘记了。
当时我用的时候,是没有问题的。

欢迎您提供您的解决方法! 3 楼 qingzhishuishou 2012-04-20   params: {  
14.                secondLevelStorageId: secondLevelStorageId,  
15.                thirdLevelStorageId: thirdLevelStorageId,  
16.                operate: operate  
17.            } 
里面的参数代表什么啊?老是报内存不够 4 楼 qimo601 2012-04-28   qingzhishuishou 写道 params: {  
14.                secondLevelStorageId: secondLevelStorageId,  
15.                thirdLevelStorageId: thirdLevelStorageId,  
16.                operate: operate  
17.            } 
里面的参数代表什么啊?老是报内存不够

这几个参应该只是两个Id值,和一个操作方法字符串

应该不是它占得内存

热点排行