Asp.net GridView 导出Excel

最近有点空就喜欢去研究研究GridView的使用,所以你如果是想要学习这方面的东西可以来参观参观。^_^

这里说明一下GridViewExcel的导出实现。平时你看到的GridView导出Excel代码可能很多了,但是我这里的肯定和你平时看到的不同,先看一下我的截图吧:

看出来了吧,我的导出是有条件可以选择的。你是想到处当前页面的数据呢还是想到处全部数据,或者是前30条数据。。。。

先说明一下我的实现原理吧。

 

当点击导出按钮时会去判断RadioButtonlist当前值然后去选择应该导出哪些数据到Excel。代码如下:

 

 

导出GrivView到Excel的方法:

 

            public static void Export(string fileName, GridView gv,int top)  
  1.    {  
  2.        HttpContext.Current.Response.Clear();  
  3.        HttpContext.Current.Response.AddHeader(  
  4.            "content-disposition"string.Format("attachment; filename={0}", fileName));  
  5.        HttpContext.Current.Response.ContentType = "application/ms-excel";  
  6.   
  7.        using (StringWriter sw = new StringWriter())  
  8.        {  
  9.            using (HtmlTextWriter htw = new HtmlTextWriter(sw))  
  10.            {  
  11.                //  Create a form to contain the grid  
  12.                Table table = new Table();  
  13.   
  14.                //  add the header row to the table  
  15.                if (gv.HeaderRow != null)  
  16.                {  
  17.                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);  
  18.                    table.Rows.Add(gv.HeaderRow);  
  19.                }  
  20.   
  21.                //  add each of the data rows to the table  
  22.                if (top == 30)  
  23.                {  
  24.                    for (int i = 0; i <= 29; i++)  
  25.                    {  
  26.                        GridViewRow row = gv.Rows[i];  
  27.                        GridViewExportUtil.PrepareControlForExport(row);  
  28.                        table.Rows.Add(row);  
  29.                    }  
  30.                }  
  31.                else  
  32.                {  
  33.                     
  34.                    foreach (GridViewRow row in gv.Rows)  
  35.                    {  
  36.                        GridViewExportUtil.PrepareControlForExport(row);  
  37.                        table.Rows.Add(row);  
  38.                    }  
  39.                }  
  40.                //  add the footer row to the table  
  41.                if (gv.FooterRow != null)  
  42.                {  
  43.                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);  
  44.                    table.Rows.Add(gv.FooterRow);  
  45.                }  
  46.   
  47.                //  render the table into the htmlwriter  
  48.                table.RenderControl(htw);  
  49.   
  50.                //  render the htmlwriter into the response  
  51.                HttpContext.Current.Response.Write(sw.ToString());  
  52.                HttpContext.Current.Response.End();  
  53.            }  
  54.        }  
  55.    }  
  56.   
  57.   
  58.    /// <summary>  
  59.    /// Replace any of the contained controls with literals  
  60.    /// </summary>  
  61.    /// <param name="control"></param>  
  62.    private static void PrepareControlForExport(Control control)  
  63.    {  
  64.        for (int i = 0; i < control.Controls.Count; i++)  
  65.        {  
  66.            Control current = control.Controls[i];  
  67.            if (current is LinkButton)  
  68.            {  
  69.                control.Controls.Remove(current);  
  70.                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));  
  71.            }  
  72.            else if (current is ImageButton)  
  73.            {  
  74.                control.Controls.Remove(current);  
  75.                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));  
  76.            }  
  77.            else if (current is HyperLink)  
  78.            {  
  79.                control.Controls.Remove(current);  
  80.                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));  
  81.            }  
  82.            else if (current is DropDownList)  
  83.            {  
  84.                control.Controls.Remove(current);  
  85.                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));  
  86.            }  
  87.            else if (current is CheckBox)  
  88.            {  
  89.                control.Controls.Remove(current);  
  90.                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));  
  91.            }  
  92.   
  93.            if (current.HasControls())  
  94.            {  
  95.                GridViewExportUtil.PrepareControlForExport(current);  
  96.            }  
  97.        }  

 

 

 本文转载处:http://blog.csdn.net/dujingjing1230/archive/2009/10/29/4744428.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值