当前位置:编程学习 > C#/ASP.NET >>

ASP.NET 导出 EXCEL 问题!不知道这样的写法哪里有问题,导出的全是乱码!请高手查看


#region 结构类
    public class AppStructure  
    {
       public string App_EyeAppoinID;
       public string App_EyeAppoinName;
       public string App_EyeAppoinNl;
       public string App_EyeAppoinXb;
       public string App_EyeAppoinShop;
       public string App_EyeAppoinDzyj;
       public string App_EyeAppoinQq;
       public string App_EyeAppoinDz;
       public string App_EyeAppoinYzbm;
       public string App_EyeAppoinJsleft;
       public string App_EyeAppoinJsright;
       public string App_EyeAppoinYx;
       public string App_EyeAppoinXm;
       public string App_EyeAppoinXmText;
       public string App_EyeAppoinYyDate;
       public string App_EyeAppoinTbDate;
       public string App_EyeAppoinZt;
       public string App_EyeAppoinPz;
       public string App_EyeAppoinSfzh;
       public string App_EyeAppoinFs;
       public string App_EyeAppoinFsText;
       public string App_EyeAppoinIp;
    }
    #endregion

    public static void AppE(int AppID)
    {
        SqlConnection con = DataCon.DataBaseCon();
        con.Open();

        SqlCommand cmd = new SqlCommand("EyeAppoin_GetModel", con);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter ApId = new SqlParameter("@EyeAppoinID", SqlDbType.Int, 4);
        ApId.Value = AppID;
        cmd.Parameters.Add(ApId);

        SqlDataReader sdr = cmd.ExecuteReader();
        AppStructure Ap = new AppStructure();

        if (sdr.Read())
        {
                        Ap.App_EyeAppoinID = sdr["EyeAppoinID"].ToString();
            Ap.App_EyeAppoinName = sdr["EyeAppoinName"].ToString();
            Ap.App_EyeAppoinNl = sdr["EyeAppoinNl"].ToString();
            Ap.App_EyeAppoinXb = sdr["EyeAppoinXb"].ToString();
            Ap.App_EyeAppoinShop = sdr["EyeAppoinShop"].ToString();
            Ap.App_EyeAppoinDzyj = sdr["EyeAppoinDzyj"].ToString();
            Ap.App_EyeAppoinQq = sdr["EyeAppoinQq"].ToString();
            Ap.App_EyeAppoinDz = sdr["EyeAppoinDz"].ToString();
            Ap.App_EyeAppoinYzbm = sdr["EyeAppoinYzbm"].ToString();
            Ap.App_EyeAppoinJsleft = sdr["EyeAppoinJsleft"].ToString();
            Ap.App_EyeAppoinJsright = sdr["EyeAppoinJsright"].ToString();
            Ap.App_EyeAppoinYx = sdr["EyeAppoinYx"].ToString();
            Ap.App_EyeAppoinXm = sdr["EyeAppoinXm"].ToString();
            Ap.App_EyeAppoinXmText = sdr["EyeAppoinXmText"].ToString();
            Ap.App_EyeAppoinYyDate = sdr["EyeAppoinYyDate"].ToString();
            Ap.App_EyeAppoinTbDate = sdr["EyeAppoinTbDate"].ToString();
            Ap.App_EyeAppoinZt = sdr["EyeAppoinZt"].ToString();
            Ap.App_EyeAppoinPz = sdr["EyeAppoinPz"].ToString();
            Ap.App_EyeAppoinSfzh = sdr["EyeAppoinSfzh"].ToString();
            Ap.App_EyeAppoinFs = sdr["EyeAppoinFs"].ToString();
            Ap.App_EyeAppoinFsText = sdr["EyeAppoinFsText"].ToString();
            Ap.App_EyeAppoinIp = sdr["EyeAppoinIp"].ToString();
         }

        string Appth = System.Web.HttpContext.Current.Server.MapPath("../Excel/");
        Encoding code = Encoding.GetEncoding("gb2312");

        

        string Temp = System.Web.HttpContext.Current.Server.MapPath("../Template/Template.xls");
        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";
        try
        {
            sr = new StreamReader(Temp, code);
            str = sr.ReadToEnd();
        }
        catch (Exception exp)
        {
            System.Web.HttpContext.Current.Response.Write("读模板时出错信息为:" + exp.Message);
            System.Web.HttpContext.Current.Response.End();
            sr.Close();
            sr.Dispose();
        }

        string xlsfilename = Ap.App_EyeAppoinID + "Date.xls";

        str = str.Replace("#lihaibin#", Ap.App_EyeAppoinID);



        try
        {
            _123_Service.DeleteFile(Appth, xlsfilename);
            sw = new StreamWriter(Appth + xlsfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            System.Web.HttpContext.Current.Response.Write("写参数时出错信息为:" + ex.Message);
            System.Web.HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
            sw.Dispose();
        }


=====================
以上是我的代码
导出的文件打开全是乱码,这是怎么地咯? --------------------编程问答--------------------



    int i; 
    int j; 
    string sqtjzhi = ""; 
    //权限判断 
    if (m01 == "7") { 
        //只有权限到社区的时候才提示要随访的人数 
        sqtjzhi = " and x.sq=" + m02 + " and jd='" + ljd + "'"; 
        //sql = "select x.name,case x.H_bzid when 'wz_tjxx_zh_djb.aspx' then 'tj/wz_tjxx_zh_djb.aspx' when '../hebeitj/hb_tjxx_djb.aspx' then 'hebeitj/hb_tjxx_djb.aspx' when '../wsbxm/tjxx_hch.aspx' then 'wsbxm/tjxx_hch.aspx' end as H_bzid,d.presj ,d.nextsj,d.bz,d.jcid,d.Avgsbp,d.Avgdbp from ddsf d left join xyjcb x on d.jcid=x.id where d.nextsj <='" & Now.ToShortDateString.ToString & " ' and x.status=0 " & sqtjzhi & " order by d.nextsj" 
        sql = "select x.name as 姓名,d.Avgsbp as 平均血压SBP,d.Avgdbp as 平均血压DBP,convert(varchar(10),d.presj,121) as 上次随访时间 ,convert(varchar(10),d.nextsj,121) as 下次随访 from ddsf d left join xyjcb x on d.jcid=x.id where d.nextsj <='" + Today.AddDays(14).ToString + " ' and x.status=0 " + sqtjzhi + " " + strssrq + " order by d.nextsj"; 
        
        SqlDataAdapter adapter = new SqlDataAdapter(sql, con); 
        DataSet ds = new DataSet(); 
        adapter.Fill(ds); 
        DataGrid1.DataSource = ds.Tables(0); 
        DataGrid1.DataBind(); 
        con.Close(); 
        con.Dispose(); 
        con = null; 
        
        Response.ContentType = "application/vnd.ms-excel"; 
        // 从Content-Type header中去除charset设置 
        
        Response.Charset = ""; 
        
        // 关闭 ViewState 
        this.EnableViewState = false; 
        System.IO.StringWriter tw = new System.IO.StringWriter(); 
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 
        // 获取control的HTML 
        DataGrid1.RenderControl(hw); 
        // 把HTML写回浏览器 
        Response.Write(tw.ToString()); 
        Response.End(); 
        Response.AddHeader("content-disposition", "attachment;filename=myfile.xls"); 
        
        
    } 

--------------------编程问答-------------------- 大哥,我要的不是代码,我想知道我哪里做错了,才生成出的乱码 --------------------编程问答-------------------- up --------------------编程问答-------------------- up --------------------编程问答-------------------- 参考:

http://blog.csdn.net/insus/archive/2008/03/31/2231945.aspx

如果一定要问理由,你尝试把导出的文件存在服务器一个临时文件夹内,再做下载,就行了. --------------------编程问答-------------------- up --------------------编程问答-------------------- up --------------------编程问答-------------------- config.xml 中的 語言設置應該 不對。如果要顯示 繁體 請設置成UTF-8,顯示簡體GB2312 在 <appSettings>下設置。
    
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,