Ext.Net 1.x_Ext.Net.GridPanel - Excel导出方法实现

Ext.net官网上有Excel导出的方法,在Version1.5和目前最新的Version2.0版本上都有该方法。但是都没有列头……下面介绍的方法也是在网上找的。

别问我为什么不放一个原著的URL。那个家伙自己也是转的,就这样转啊转。到我这我都不晓得那个是原作者了……关键它帮助了我,在这整理下,留个脚印。

 

代码:

复制代码
public void ToExcel(GridPanel gp, string fileName, DataTable dt, HttpResponse resp)
        {
            resp.Clear();
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
            resp.ContentType = "application nd.ms-excel";
            string colHeaders = "", ls_item = "";
            int i = 0;
            int cl = gp.ColumnModel.Columns.Count;
            string columns = ""; 
            //输出表头  取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符 
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列,加n
                {
                    if (!gp.ColumnModel.Columns[i].Hidden)
                    {
                        colHeaders += gp.ColumnModel.Columns[i].Header;
                        columns += gp.ColumnModel.Columns[i].DataIndex + ",";
                    }
                    colHeaders += "\n";
                }
                else
                {
                    if (!gp.ColumnModel.Columns[i].Hidden && gp.ColumnModel.Columns[i].ColumnID != "update")
                    {
                        colHeaders += gp.ColumnModel.Columns[i].Header + "\t";
                        columns += gp.ColumnModel.Columns[i].DataIndex + ",";
                    }
                }
            }
            resp.Write(colHeaders);
            if (columns != "") columns = columns.Substring(0, columns.LastIndexOf(","));
            string[] columnall = columns.Split(',');
            cl = columnall.Length;
            //逐行处理数据   
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据     
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))
                    {
                        ls_item += EditString(dt.Rows[j][columnall[i].ToString()].ToString()) + "\n";
                    }
                    else
                    {
                        ls_item += EditString(dt.Rows[j][columnall[i].ToString()].ToString()) + "\t";
                    }
                }
                resp.Write(ls_item);
                ls_item = "";
            }
            Response.Flush();
            resp.End();
        }
        /// 
        /// 处理字符问题
        /// 
        /// 
        /// 
        private static string EditString(string text1)
        {
            if (text1.IndexOf('\r') != -1)
            {
                text1 = text1.Replace('\r', ' ');
            }
            if (text1.IndexOf('\n') != -1)
            {
                text1 = text1.Replace('\n', ' ');
            }
            if (text1.IndexOf('\t') != -1)
            {
                text1 = text1.Replace('\t', ' ');
            }
            if (text1.IndexOf("'") != -1)
            {
                text1 = text1.Replace("'", "");
            }
            if (text1.IndexOf(@"""") != -1)
            {
                text1 = text1.Replace(@"""", "");
            }
            return text1;
        }
复制代码

导出按钮

复制代码
 /// 
        /// 导出Excel
        ///  
        protected void ToExcel(object sender, EventArgs e) 
        { 
            DataTable dtSource = _createPageManager.GetTrackBillData(mouldId, 2, "", 1, Cache); 
            ToExcel(gpExec, "test", dtSource, Response);  
        }
复制代码

 

前台代码

 <ext:Button ID="btnExportExcel" AutoPostBack="true" OnClick="ToExcel"   runat="server" Text="导出Excel"> 
                                                                                <Listeners>
                                                                                    <Click Fn="saveData" />
                                                                                Listeners>
                                                                            ext:Button>

注意: AutoPostBack="true" 别忘了,否则你懂的

saveData
方法
           //导出Excel
           var saveData = function () {
                GridData.setValue(Ext.encode(gpExec.getRowsValues({selectedOnly : false}))); 
           };
        
gpExec你的GridPanel主键ID
转自:http://www.cnblogs.com/PEPE/archive/2012/07/19/2599269.html

你可能感兴趣的:(前端设计,Ext.Net)