我的页面上有一个网格视图,我想将其导出到Excel工作表,下面是我为执行此任务而编写的代码,这里我已经将数据集传递给绑定网格的方法,并且btnExcelExport是按钮会将表格内容导出到Excel工作表中:
private void BindGridView(DataSet ds)
{
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
GVUserReport.DataSource = ds;
GVUserReport.DataBind();
btnExcelExport.Visible = true;
}
}
}
protected void btnExcelExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition","attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GVUserReport.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
现在,当我进行调试时,我发现网格已成功绑定,但是当尝试将其导出到Excel时,出现此错误:
“ Microsoft
JScript运行时错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。”