用这样的
public void ToExcel(System.Web.UI.Control ctl)
    {        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }
可以直接把gridview显示出来的当前页下载,
如果有分页的,不是当前页的没在下载的内容中。
要怎么处理才能把所有内容下载 

解决方案 »

  1.   

    从数据库中读取,导出到Excel
      

  2.   

    我写了个函数,可以实现将dataTable下载到Excel,并定义title,可留下邮箱发你一份
      

  3.   

    重新放一個同樣GridView,不設分頁 ,設置Visible=false,然后用該GridView直接導出,省事。
    不過數據太多的話,就要考慮頁面的性能了。
      

  4.   

    thank you
    e-mail:[email protected]
      

  5.   

    现在还发现了,用我上面的下载,在韩文系统中,下载下来的excel中的中文都是乱码
      

  6.   

    呵呵,可以的,直接把数据源导入到Excel就可以了.
      

  7.   

    我也碰到 跟LZ 一样的问题,  我是Repeater里的数据导出来 也是  只能导出 当前页的数据, 不能获取所有的数据乱码的情况   Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 加这一句就可以了   
    2楼的大哥能不能给我发一份??
      

  8.   

      GridView1.AllowPaging = false;//设置GridView控件不能分页       databind();
            exc.ToExcel(GridView1);
            GridView1.AllowPaging = true;//恢复GridView控件分页      databind();//数据绑定函数这样可以下载全部的了,但是
     public void ToExcel(System.Web.UI.Control ctl)
        {        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.Charset = "GB2312";        HttpContext.Current.Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }
    改成这样,在韩文系统中下载后,中文还是变成乱码