private void Export(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();    }
    public override void VerifyRenderingInServerForm(Control control)
    {    }    protected void ExporttoExcel_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", "Report.xls");
    }Gridview导出到Excel

解决方案 »

  1.   

        private void Export(string FileType, string FileName)
        {
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            GridView1.AllowPage = false;//设置为不分页,同时数据绑定时要保证绑定的是全部数据。
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();    }
      

  2.   

    可以设置导出的时候不分页,导出完毕后回归分页。
    GridView1.AllowPaging = false; 
            GridView(); 
            ExcelHelper.DataToExcel(GridView1); //导出事件
            GridView1.AllowPaging = true; 
            GridView(); 
      

  3.   

    我加多了2行打开和禁止分页的代码,可是导出来数据还是只有一页,且还出现有下一页的按钮的背影    private void Export(string FileType, string FileName)
        {
            GridView1.AllowPaging = false;//设置为不分页
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentType = FileType;
            this.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            
            GridView1.RenderControl(hw);
            Response.Write(tw.ToString());
            Response.End();
            GridView1.AllowPaging = true;//设置为为分页    }
      

  4.   

    在你GridView1执行绑定之前设置不分页。
    绑定过后再设置我记得是无效的。
      

  5.   

    方案一:导出时设置不分页.
    GridView1.AllowPage = false;方案二:导出的时候重新到数据库里面查一遍,不使用界面上的数据(此方案能够保证数据是最新的.)
    当然,在方案一上你做了异步 定时刷新也是可以的.