从DATEVIEW导出EXCEL文件

  protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
 
   DataView dv = (DataView)Session["xpdvlist"];
   Response.Clear();
   Response.AppendHeader("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;
   GridView1.DataSource = dv;
   GridView1.DataBind();
   GridView1.RenderControl(htmlWrite);
   Response.Output.Write(stringWrite.ToString());
   Response.End();
    }
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {  /* Confirms that an HtmlForm control is rendered for the specified ASP.NET     server control at run time. */
    }

在ASPX文件的@ Page中加上EnableEventValidation = "false"。
EnableEventValidation 属性 获取或设置一个值,该值指示是否应验证回发事件数据。

你可能感兴趣的:(导出Excel)