GridView中的数据导出到Excel

将页面中的数据绑定控件(如GridView、ListView等)中的数据导出到Excel中是个很常用的功能,Google相关资料后总结如下:

一、自定义一个方法:ToExcel(Control ctl, string FileName)

这个方法就是将数据绑定控件中的数据导出到Excel。

private void ToExcel(Control ctl, string FileName)
    {
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }

二、双击LinkButton,在按钮中写入如下代码:

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;
        GridView1.AllowSorting = false;
        bang();
        ToExcel(GridView1, "data.xls");
        GridView1.AllowPaging = true;
        GridView1.AllowSorting = true;
        bang();
       
    }

  三、当然了,还有自定义的绑定方法bang()。写这个bang()就不用在绑定数据时写一大堆的代码了。bang()代码略。

  

  其实将数据导出为Excel应该是很常用的一个功能,为避免在不同的页面中都写一遍代码,我们可以将其做成一个类模块,保存在App_Code文件夹中,这样需要用到的时候就可以直接引用了。

App_Code文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//
using System.Web.UI;

public class toExcel
{
public toExcel()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
    public Control ctl;
    public string FileName;
    public void exportExcel(Control ctl, string FileName)
    {        
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }
}

然后在CS文件中引用类模块:

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        GridView1.AllowPaging = false;//取消分页
        GridView1.AllowSorting = false;//取消排序
        bind();

        toExcel Export=new toExcel();
        Control ctl = Export.ctl = GridView1;
        string FileName = Export.FileName = "我的表格.xls";
        Export.exportExcel(ctl, FileName);

        GridView1.AllowPaging = true;//恢复分页
        GridView1.AllowSorting = true;//恢复排序
        bind();
    }

 

注意!很容易出现这个错误提示:

 

类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内

 

如果是这样,需要重载一个方法(方法中什么都不写即可。):

 

 public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

这样就不会有错误了。

运用类模块后,在任何需要导出到excel的页面中只需要简单的引用就可以了,主要就是:

     toExcel Export=new toExcel();
        Control ctl = Export.ctl = GridView1;
        string FileName = Export.FileName = "我的表格.xls";
        Export.exportExcel(ctl, FileName);

 

另一篇导出到Excel的文章:http://www.cnblogs.com/ibgo/p/3531799.html

posted @ 2012-04-19 10:15  ibg  阅读(414)  评论(3编辑  收藏  举报