我的代码:
前台
 <asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" 
                                    AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="5"                OnRowCancelingEdit="GridView_RowCancelingEdit" OnRowEditing="GridView_RowEditing" 
                                    OnRowUpdating="GridView_RowUpdating"  Height="400px" DataKeyNames="qq" 
                                     >
后台:
 protected void Button1_Click(object sender, EventArgs e)
    {
        
       
       
        Export("application/ms-excel", "用户信息表.xls");
        
 }
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);
        GridView.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
       }
结果到处时候:只能够导出当前页面内容
1 2 3
点击1,导出1表的内容!
谢谢大家帮忙!!

解决方案 »

  1.   


     public static void DataTable2Excel(System.Data.DataTable dtData, String FileName)//dt导出到EXCEL
        {
            System.Web.UI.WebControls.GridView dgExport = null;
            //当前对话 
            System.Web.HttpContext curContext = System.Web.HttpContext.Current;
            //IO用于导出并返回excel文件 
            System.IO.StringWriter strWriter = null;
            System.Web.UI.HtmlTextWriter htmlWriter = null;        if (dtData != null)
            {
                //设置编码和附件格式 
                System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);//作用是方式中文文件名乱码
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF7;
                curContext.Response.Charset = "GB2312";            //导出Excel文件 
                strWriter = new System.IO.StringWriter();
                htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);            //为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的GridView 
                dgExport = new System.Web.UI.WebControls.GridView();
                dgExport.DataSource = dtData.DefaultView;
                dgExport.AllowPaging = false;
                dgExport.DataBind();            //下载到客户端 
                dgExport.RenderControl(htmlWriter);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
            }
        }
    调用
    类名.DataTable2Excel(gridview绑定的DataTable名,导出的excel文件名称)
      

  2.   

    导出之前应该设置GridView.AllowPaging = false吧,导出完成后再改回原状态应该可以吧
      

  3.   

    2楼,写完C#类之后,在asp后台的button click事件怎么引用那个类的?
    这个方法不错,是不是可以再详细一些呢?把代码给一下,我是初学者!谢啦~!~~
      

  4.   

    1楼,运行时报错: 
     DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
    DbHelperSQL是指什么?
    这句语句的作用是什么?
    小弟我没辙了1
      

  5.   

    导出之前GridView.AllowPaging = false
    导出之后GridView.AllowPaging = true