Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.Flush();
        Response.End();
//我用的是这种方法 怎么转化?

解决方案 »

  1.   

    使用你这样的方式是不行的,你这样其实只是把html格式用excel读出来了而已;
      

  2.   

    利用GridView输出,经测试,有的输出有乱码public bool LendOutExcel(string strFileName, DataTable DT)
            {
                try
                {
                    //清除Response缓存内容
                    HttpContext.Current.Response.Clear();
                    //缓存输出,并在完成整个响应之后将其发送
                    HttpContext.Current.Response.Buffer = true;
                    //strFileName指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
                    strFileName = strFileName + ".xls";
                    //设置输出流的HTPP字符集,确定字符的编码格式
                    //HttpContext.Current.Response.Charset = "UTF-8";
                    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开 
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName));
                    //Response.ContentType指定文件类型.可以为application/ms-excel,application/ms-word,application/ms-txt,application/ms-html或其他浏览器可直接支持文档 
                    HttpContext.Current.Response.ContentType = "application/ms-excel";                //用GridView输出
                    GridView dv = new GridView();
                    dv.DataSource = DT;
                    dv.DataBind();
                    try
                    {
                        dv.Page.EnableViewState = false;
                    }
                    catch
                    { }
                    System.IO.StringWriter swBody = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter hwBody = new System.Web.UI.HtmlTextWriter(swBody);
                    //dv表示输出GridView,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
                    dv.RenderControl(hwBody);
                    //消除乱码特别设定,非常规方法
                    string strExcel = "";
                    strExcel = "";
                    strExcel += hwBody.InnerWriter.ToString();
                    HttpContext.Current.Response.Write(strExcel);
                    HttpContext.Current.Response.End();                return true;
                }
                catch
                {
                    return false;
                }
            }
      

  3.   

    不好意思!!没看仔细!
    这个要你手写excel了吧!
    有那么智能的东东吗??
    自己转换~~