http://www.google.cn/search?client=aff-cs-worldbrowser&forid=1&ie=utf-8&oe=UTF-8&hl=zh-CN&q=%E6%80%8E%E6%A0%B7%E5%B0%86DataGrid%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E5%AF%BC%E5%85%A5Excel%E4%B8%AD+

解决方案 »

  1.   

    try
    {
    Response.Clear(); 
    Response.Buffer= true;  
    Response.Charset="GB2312";    
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文
    Response.AppendHeader("Content-Disposition","attachment;filename=test.xls"); 
    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。  this.EnableViewState = false;    
    CultureInfo myCItrad = new CultureInfo("ZH-CN",true);

    HtmlGenericControl div=new HtmlGenericControl();
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
    div.InnerHtml = oStringWriter.ToString();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        
    // 下载页面显示的
    DataGrid1.RenderControl(oHtmlTextWriter);  // LZ 注意此处DataGrid1
    div.InnerHtml =oStringWriter.ToString();
    Response.Write(div.InnerText); 
    Response.End();
    }
    catch( Exception ex)
    {
    MsgBox("",ex.Message);
    }
      

  2.   

    我上次用到的地址推荐给你:http://bbs.92jp.com/dispbbs.asp?boardID=4&ID=13&page=1
      

  3.   


    MsgBox是我自己写的一个类,用于弹出对话框.
      

  4.   

    protected void btnExcel_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            string FileName = "XXXX.xls";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.UTF8Encoding.UTF8).ToString());
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/ms-excel";
            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.datagrid1.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();    }有点要注意,DG不能带分页和按钮列,建议最好重新绑定个不带分页和按钮列的DG导入
      

  5.   

    哦,还有DG要放在带有 runat="server"的控件里