GridView导出数据到Excel

来源:互联网 发布:汉字笔画顺序查询软件 编辑:程序博客网 时间:2024/04/30 13:17

private void ExportToExcel()
    {

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

        string FileName = "文件名.xls";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
        Response.ContentType = "application/ms-excel";
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.AllowPaging = false;
        GVDataBind();
        GridView1.Columns[6].Visible = false;
        GridView1.Columns[7].Visible = false;
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
        GridView1.AllowPaging = true;
        GVDataBind();
    }

 

//这个方法必须加,否则会报错!

 public override void VerifyRenderingInServerForm(Control control)
    {
    }