关于GridView导出Excel的一些问题(采用Ajax出现的的问题及解决方法)
以前用GridView导出Excel没碰到什么问题,(注: enableEventValidation 属性必须设置为 "false"——直接在页面设置或在配置文件里设置),但采用Ajax后就出问题了,错误提示:Details Error parsing near '<div><table cells'.
原因:
导出Excel的按钮放置在<UpdatePanel></UpdatePanel>里面,点击按钮后只输出了“<div><table cells”,生成的容器表--html代码被截断了
解决方法:
放到<UpdatePanel></UpdatePanel>外面(注:不要放在任何的<UpdatePanel> </UpdatePanel>中)
以下为导出Excel方法:
/// <summary>
/// 重载方法——VerifyRenderingInServerForm (必须添加)
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
/// <summary>
/// 生成报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButtonexcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
原因:
导出Excel的按钮放置在<UpdatePanel></UpdatePanel>里面,点击按钮后只输出了“<div><table cells”,生成的容器表--html代码被截断了
解决方法:
放到<UpdatePanel></UpdatePanel>外面(注:不要放在任何的<UpdatePanel> </UpdatePanel>中)
以下为导出Excel方法:
/// <summary>
/// 重载方法——VerifyRenderingInServerForm (必须添加)
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
/// <summary>
/// 生成报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ImageButtonexcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}