为什么导出excel时会把web form的其他非datagrid控件(例如Label)也导入到excel里面去?

解决方案 »

  1.   

    bool CurrCtlVisible=ctl.Visible;
    ctl.Visible=true;  
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");//!
    string tt=HttpContext.Current.Response.ContentType;
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
    HttpContext.Current.Response.ContentType ="application/ms-excel/ms-  word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
    // ctl.Page.EnableViewState = false; 
    System.IO.StringWriter tw = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
    ctl.RenderControl(hw); 
    HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
    HttpContext.Current.Response.Write(tw.ToString()); 
    HttpContext.Current.Response.End();

    ctl.Page.EnableViewState = true; 
    ctl.Visible=CurrCtlVisible;
      

  2.   

    我代入几个datagrid进去,只有一个达到效果,其他的都是excel包含非datagrid内容。
    搞到我郁闷死了。不知问题出在哪里,同样调用一个函数却有不同效果。
      

  3.   

    在它后面HttpContext.Current.Response.Clear();
    在加一个Response.BufferOutput=true;试一下