解决ASP.NET中从GridView导出Execel出现的“空间GridView必须置于有runat=server的窗体标记中”问题

来源:互联网 发布:sql经典50题 编辑:程序博客网 时间:2024/05/16 04:55

//首先在后置代码中添加这个事件,

//public override void VerifyRenderingInServerForm(Control control)
//    {
        //base.VerifyRenderingInServerForm(control);
//    }然后在Web.config文件中,修改<pages>.......</pages>为

//          <pages enableEventValidation="false"></pages>

//以下是实现导出到Execl功能的代码;

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

    protected void Button1_Click(object sender, EventArgs e)
    {//导出Execl
        string style = @"<style> .text { mso-number-format:/@; } </script> ";

        Response.ClearContent();

        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AddHeader("content-disposition", "attachment; filename=Excel.xls");

        Response.ContentType = "application/excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        GridView1.RenderControl(htw);

        Response.Write(style);

        Response.Write(sw.ToString());

        Response.End();

     }

原创粉丝点击