以下导入代码应该是gv1表数据,但导出的结果却是整个网页,请各位大侠指点下(注:我的gridview是做在用户控件里的,用于sharepoint网站)protected void btn_Excel_Click(object sender, EventArgs e)
    {
        try
        {
            string xlsFile = "Excel";
            HttpContext.Current.Response.Clear();
          
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" +      System.Web.HttpUtility.UrlEncode(xlsFile, System.Text.Encoding.UTF8) + ".xls");            HttpContext.Current.Response.Charset = "GB2312";
            Response.ContentEncoding = Encoding.GetEncoding("UTF-8");
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
           
                        this.gv1.RenderControl(htmlWrite);
          
          
            
            Response.Write(stringWrite.ToString());
            Response.End();
            
             }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message + "')</script>");
        }
    }

解决方案 »

  1.   

    GridView控件实现数据导入Excel(全部数据) 

    C# code45     private void Export(string FileType, string FileName)
    46     {
    47         GridView.AllowPaging = false;
    48         GridView.AllowSorting = false;
    49 
    50         BindData();
    51 
    52         Response.Clear();
    53         Response.Charset = "GB2312";
    54         Response.ContentEncoding = System.Text.Encoding.UTF7;
    55         Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
    56         Response.ContentType = FileType;
    57         this.EnableViewState = false;
    58         StringWriter tw = new StringWriter();
    59         HtmlTextWriter hw = new HtmlTextWriter(tw);
    60         GridView.RenderControl(hw);
    61         Response.Write(tw.ToString());
    62         Response.End();
    63     }
    64 
    65     public override void VerifyRenderingInServerForm(Control control)
    66     {
    67 
    68     }
      

  2.   


    namespace Common
    {
        /// <summary>
        /// 数据绑定委托
        /// </summary>
        public delegate void delegateBindData();    /// <summary>
        /// Excel处理类
        /// </summary>
        public class ExcelProcess
        {
            public delegateBindData delbinddata;        /// <summary>
            /// 绑定数据
            /// </summary>
            public void BindData()
            {
                if(delbinddata!=null)
                    delbinddata();
            }        /// <summary>
            /// GridView导出数据到Excel
            /// </summary>
            /// <param name="Charset">输出流的HTTP字符集</param>
            /// <param name="ContentEncoding">输出流的HTTP字符集编码</param>
            /// <param name="FileName">导出的文件名</param>
            /// <param name="ContentType">文件类型 默认application/ms-excel</param>
            /// <param name="gvList">GridView控件</param>
            public void ToExcel(string Charset, Encoding ContentEncoding, string FileName, string ContentType, System.Web.UI.WebControls.GridView gvList)
            {
                if (string.IsNullOrEmpty(Charset))
                    Charset = "GB2312";
                if (ContentEncoding == null)
                    ContentEncoding = Encoding.GetEncoding("GB2312");
                if(string.IsNullOrEmpty(ContentType))
                    ContentType = "application/ms-excel";            System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.Buffer = true;
                System.Web.HttpContext.Current.Response.Charset = Charset;
                System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + "");
                // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
                System.Web.HttpContext.Current.Response.ContentEncoding = ContentEncoding;
                System.Web.HttpContext.Current.Response.ContentType = ContentType;//设置输出文件类型为excel文件。 
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);            gvList.AllowPaging = false;
                BindData();            gvList.RenderControl(oHtmlTextWriter);
                System.Web.HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
                System.Web.HttpContext.Current.Response.Flush();
                System.Web.HttpContext.Current.Response.End();            gvList.AllowPaging = true;
                BindData();
            }
            /// <summary>
            /// GridView导出数据到Excel
            /// </summary>
            /// <param name="FileName">导出的文件名</param>
            /// <param name="gvList">GridView控件</param>
            public void ToExcel(string FileName, System.Web.UI.WebControls.GridView gvList)
            {
                ToExcel(string.Empty, null, FileName, string.Empty, gvList);
            }
        }
    }
      

  3.   

    调用:
    /// <summary>
        /// 此方法不可少(以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm控件)
        /// </summary>
        public override void VerifyRenderingInServerForm(Control control)
        { }
        protected void btnExportDetail_Click(object sender, EventArgs e)
        {
            ExcelProcess ep = new ExcelProcess();
            ep.delbinddata = new delegateBindData(BindData);
            ep.ToExcel("UTF-8", Encoding.UTF8, "test.xls", "application/ms-excel", gvDetail);
        }
        
        public void BindData()
        {
            //绑定数据
        }
      

  4.   

    写在用户控件里的gridview,不能重载VerifyRenderingInServerForm(Control control)
      

  5.   

    http://space.itpub.net/12639172/viewspace-485862这里面有介绍用户控件上的GridView如何导出到Excel表,但问题是sharepoint网站中的页面加不了EnableEventValidation = "false"属性,不知道有这方面经验的高手,是怎么解决的