思想决定人生,态度改变一切

成功者找方法,失败者找借口! 做事先做人;安分做人,本分做事!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
导出GridView到Excel中的关键之处
用法: ToExcel(GVStaff, TextBox1.Text);

public static void ToExcel(System.Web.UI.Control ctl,string FileName)
        
{
            HttpContext.Current.Response.Charset 
="UTF-8";
            HttpContext.Current.Response.ContentEncoding 
=System.Text.Encoding.Default;
            HttpContext.Current.Response.ContentType 
="application/ms-excel";
            HttpContext.Current.Response.AppendHeader(
"Content-Disposition","attachment;filename="+""+FileName+".xls");
            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();
        }
        
     
必须有下面这句!否则不会通过!
public override void VerifyRenderingInServerForm(Control control)
    
{
        
// Confirms that an HtmlForm control is rendered for
    }
 

作者Blog:http:
//blog.csdn.net/downmoon/
posted on 2006-03-30 13:58  投石问路  阅读(301)  评论(1编辑  收藏  举报