将GridView中的数据导出到Excel / Word中

      在C#程序设计时,我们很常用到GridView控件来显示数据,那我们怎样才能将excel中的数据导到Excel或者Word中呢?
前提条件:你的GridView已经能正常显示数据了,不管你是用代码实现的,还是直接绑定数据库实现的。
1、如果你的GridView启动了分页功能,则要先将该功能关闭,即将allowpaging的属性设为false, AllowPaging="false";然后重新调用databind()或者你自己定义的显示函数,确保所有的数据都显示在GridView中,再导完数据之后,记得把allowpaging的属性值改回来。
2、在页面中添加一个"导出"按钮,当点击该按钮时执行导出动作。双击改按钮,编写事件处理函数,添加代码如下:在用到StringWriter类的时候,要在。aspx.cs文件的头部添加命名空间:using System.IO;

protected void Button1_Click(object sender, EventArgs e)
{       //导出到Excel中(.xls文件)
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=MyExcel2.xls");
        /*
         
导出时会弹出对话框让你确认保存位置,默认的文件名为MyExcel2.xls,
         
你可以选择保存,也可以直接打开
    
  */
        Response.ContentType = "application/excel";
       
/*
        
如果想将文件导出到word中,则将上面的"MyExcel2.xsl"改成"MyWord.doc",
        
"application/excel"改成"application/word"
       */

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        GridView1.AllowPaging = true;
}

3、重写VerifyRenderingInServerForm 函数,这样才能确保运行时为指定的ASP.NET 服务器控件呈现HtmlForm 控件;函数的内容可以为空:

public override void VerifyRenderingInServerForm(Control control)
{

}

4、当出现"只能在执行Render() 的过程中调用 RegisterForEventValidation;"错误时,参考下面网址:https://www.ezloo.com/2008/10/render_registerforeventvalidation.html 解决问题

以下是一个比较完整的实现"将GridView中的数据导出到Word中"的代码:

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;
using System.Data.SqlClient;
using System.IO;

public partial class GridView_Export_Excel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        string Constr="server=localhost; uid=test;pwd=test;database=test";
        string sqlstr="select * from my_test";
        SqlConnection con = new SqlConnection(Constr);
        SqlDataAdapter ad = new SqlDataAdapter(sqlstr, con);
        DataSet ds = new DataSet();
        ad.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment;filename=MyExcel.doc");
        Response.ContentType = "application/word";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        BindData();
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
        GridView1.AllowPaging = true;
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
}