gridview数据
单击“导出EXCEL”按钮后
1.在上面的代码中,先将gridview绑定到指定的数据源中,然后在button按钮(用来做导出到EXCEL的)的事件中,写入相关的代码。
2.使用Response.AddHeader("content-disposition","attachment;filename= exporttoexcel.xls");中的filename来指定将要导出的excel的文件名,
3.这里是exporttoexcel.xls。要注意的是,由于gridview的内容可能是分页显示的,因此,这里在每次导出excel时,先将gridview的allowpaging属性设置为 false,
4. 然后通过页面流的方式导出当前页的gridview到excel中,最后再重新设置其allowpaging属性。另外要注意的是,要写一个空的VerifyRenderingInServerForm方法(必须写),
/// <summary>
/// 导EXCEL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExcel_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);
gvValueList.AllowPaging = false;
fun.GvDataInit(sql.selEntryTable, gvValueList, ddlMonth.SelectedValue.Trim());//绑定GV
gvValueList.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
gvValueList.AllowPaging = true;
fun.GvDataInit(sql.selEntryTable, gvValueList, ddlMonth.SelectedValue.Trim());//绑定GV
}
/// <summary>
/// 导出EXCEL,要写一个空的VerifyRenderingInServerForm方法
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
/// <summary>
/// 绑定GridView数据
/// </summary>
/// <param name="sqlID"></param>
/// <param name="gv"></param>
public void GvDataInit(string sqlID, GridView gv, string Condition1)
{
try
{
DataTable dt = null;
dt = BLL.SQLHandler.SearchData(sqlID, Condition1);
if (dt != null && dt.Rows.Count > 0)
{
gv.DataSource = dt;
}
else
{
gv.PageIndex = 0;
}
gv.DataBind();
}
catch (Exception ex)
{
gv.PageIndex = 0;
throw ex;
}
}
/// 导EXCEL
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnExcel_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);
gvValueList.AllowPaging = false;
fun.GvDataInit(sql.selEntryTable, gvValueList, ddlMonth.SelectedValue.Trim());//绑定GV
gvValueList.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
gvValueList.AllowPaging = true;
fun.GvDataInit(sql.selEntryTable, gvValueList, ddlMonth.SelectedValue.Trim());//绑定GV
}
/// <summary>
/// 导出EXCEL,要写一个空的VerifyRenderingInServerForm方法
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
/// <summary>
/// 绑定GridView数据
/// </summary>
/// <param name="sqlID"></param>
/// <param name="gv"></param>
public void GvDataInit(string sqlID, GridView gv, string Condition1)
{
try
{
DataTable dt = null;
dt = BLL.SQLHandler.SearchData(sqlID, Condition1);
if (dt != null && dt.Rows.Count > 0)
{
gv.DataSource = dt;
}
else
{
gv.PageIndex = 0;
}
gv.DataBind();
}
catch (Exception ex)
{
gv.PageIndex = 0;
throw ex;
}
}
若报错提示如下:
报RegisterForEventValidation can only be called during Render()错误 .
将
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddEntrytable.aspx.cs" Inherits="AddEntrytable" %>
修改为
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="AddEntrytable.aspx.cs" Inherits="AddEntrytable" %>
方二:
protected void btnExcel_Click(object sender, EventArgs e) { gvValueList.AllowPaging = false; databind(); ToExcel(this.gvValueList, System.Web.HttpUtility.UrlEncode("test.xls", System.Text.Encoding.UTF8)); //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); //gvValueList.AllowPaging = false; //databind(); //gvValueList.RenderControl(htmlWrite); //Response.Write(stringWrite.ToString()); //Response.End(); //gvValueList.AllowPaging = true; //databind(); } 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(); } public override void VerifyRenderingInServerForm(Control control) { // Confirms that an HtmlForm control is rendered for }