DataTable dtds = GetEncashmentExcel();//查数据的方法,返回一个DataTable,里面的值正确
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.Charset = "";
            this.EnableViewState = false;
            StringWriter oStringWriter = new StringWriter();
            HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
            Response.AddHeader("content-disposition", "attachment;filename=Encashment.xls");
            DataGrid gd = new DataGrid();
            gd.DataSource = dtds;
            gd.DataBind();
            gd.RenderControl(oHtmlTextWriter);
            Response.Write(oStringWriter.ToString());
            Response.End();出现一个提示窗,很快就关掉了,时间大约不到1秒,看不到什么信息,然后就没任何反应了

解决方案 »

  1.   

    你这段是导出代码吧,
    我在VS05写导出在VS08中是可以用的,
    检查其它问题...
      

  2.   

    一样的,2003的一样用也没问题 附上2008做参考(引入命名空间)
       public static void ToExcel(System.Web.UI.Control ctl, string filename)
            {
                //Response.Clear();
                //Response.BufferOutput = true;
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.BufferOutput = true;            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8).ToString());
                HttpContext.Current.Response.Charset = "UTF-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                HttpContext.Current.Response.ContentType = "application/ms-excel";//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 System.Web.UI.HtmlTextWriter(tw);
                ctl.RenderControl(hw);
                //Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");//乱码
                HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");//乱码
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.End();
            }