我的是动态添加GRIDview,我有3个DropDownList,我想在点点导出后,根据条件,导出到EXCEL,代码如下    private void ToExcel(Control ctl, string FileName)
    {
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName);
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }    private void toExcelClk()
    {
        
        GridView1.AllowPaging = false;
        GridView1.AllowSorting = false;
        bind();        
        ToExcel(GridView1, "OFS_Data.xls");
        GridView1.AllowPaging = true;
        GridView1.AllowSorting = true;
        GridView1.DataBind();
    }
如果不条件导出没有问题,如果加了条件以后导出只显示<div></div>,请高手帮忙

解决方案 »

  1.   

     private void ToExcel(Control ctl, string FileName) 
        { 
            HttpContext.Current.Response.Charset = "UTF-8"; 
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; 
            HttpContext.Current.Response.ContentType = "application/ms-excel"; 
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName); 
            ctl.Page.EnableViewState = false; 
            System.IO.StringWriter tw = new System.IO.StringWriter(); 
            HtmlTextWriter hw = new HtmlTextWriter(tw); 
            ctl.RenderControl(hw); 
            HttpContext.Current.Response.Write(tw.ToString()); 
            HttpContext.Current.Response.End(); 
        }     private void toExcelClk() 
        { 
            
            GridView1.AllowPaging = false; 
            GridView1.AllowSorting = false; 
            bind();        
            ToExcel(GridView1, "OFS_Data.xls"); 
            GridView1.AllowPaging = true; 
            GridView1.AllowSorting = true; 
            bind(); ///////////////这个地方改一下
        }