ASP.NET中把Gridview内容导出到Excel示例

作者:袖梨 2022-06-25

第一步:先建立一个基本的aspx页面default.aspx,我们掐头去尾,只来关键的代码:

 代码如下 复制代码








Text="Export to Excel" />

第二步:在对应的在default.aspx.cs中,复制入以下代码:

 代码如下 复制代码
protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
 {
  BindData();
 }
}
private void BindData()
{
 string query = "SELECT * FROM customers";
 SqlConnection myConnection = new SqlConnection(ConnectionString);
 SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
 DataSet ds = new DataSet();
 ad.Fill(ds, "customers");
 GridView1.DataSource = ds;
 GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
 // Confirms that an HtmlForm control is rendered for
}
protected void Button1_Click(object sender, EventArgs e)
{
 Response.Clear();
 Response.AddHeader("content-disposition","attachment;filename=FileName.xls");
 Response.Charset = "gb2312";
 Response.ContentType = "application/vnd.xls";
 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
 System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);
 GridView1.AllowPaging = false;
 BindData();
 GridView1.RenderControl(htmlWrite);
 Response.Write(stringWrite.ToString());
 Response.End();
 GridView1.AllowPaging = true;
 BindData();
}
protected void paging(object sender,GridViewPageEventArgs e)
{
 GridView1.PageIndex = e.NewPageIndex;
 BindData();
}

在上述代码中,我们首先将gridview绑定到指定的数据源中,然后在button1(也就是用来控制导出的)的事件中,定义相关代码。这里使用Response.AddHeader("content-disposition","attachment;filename= exporttoexcel.xls");中的filename来指定将要导出的excel文件名,就叫做exporttoexcel.xls。要注意的是,由于gridview的内容可能是分页显示的,因此在每次导出excel文件时,需要先将gridview的allowpaging属性设置为false,然后通过页面流的方式导出当前页的gridview内容到excel中,最后再重新设置其allowpaging属性。另外要注意的是,要写一个空的VerifyRenderingInServerForm方法(必须写),以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm 控件。
下面再介绍一种方法,也可以实现GridView到Excel的内容导出。先来看一个自定义函数:

 代码如下 复制代码
public static void ToExcel(System.Web.UI.Control ctl,string FileName)
{
    HttpContext.Current.Response.Charset ="UTF-8";
    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
    HttpContext.Current.Response.ContentType ="application/ms-excel";
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+""+FileName+".xls");
    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();
}       
    
//下面这几行代码一定不要忘记,否则导入不成功!
public override void VerifyRenderingInServerForm(Control control)
{
    // Confirms that an HtmlForm control is rendered for
}

用法:ToExcel(GVStaff, TextBox1.Text);


导出Excel出错问题

以前用DataGrid导出为excel,只需要现在的代码就可以搞定了:

 代码如下 复制代码
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvMaster.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();

可现在使用gridview后,上面的代码总提示错误,后来摸索了好大会,总算找出了问题,下面是解决办法:

1、在导出界面中重载:

 代码如下 复制代码

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

2、在web.config中修改下面的配置:

 代码如下 复制代码

或者修改ASPX页面的头部定义:

 代码如下 复制代码

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"
CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

现在再去试试吧,已经可以把Gridview里的内容导出到Excel了,你可以试下哦。

相关文章

精彩推荐