华域联盟 .Net asp.net导出Excel显示中文乱码的解决方法

asp.net导出Excel显示中文乱码的解决方法

复制代码 代码如下:

  protected void btnExcel_Click(object sender, EventArgs e)

    {

        List<bUFlow.Model.orderhistory> orderlist = dal.GetOrderList2("");

        string filename = "3g流量网龄升级计划用户表";

        string name1 = filename;

        string bname = Common.MyRequest.GetBrowserName().ToLower();

        if (bname.Contains("firefox"))

        {

        }

        else if (bname.Contains("ie"))

        {

            filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);

        }

        else

        {

            filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);

        }

        HttpResponse resp = System.Web.HttpContext.Current.Response;
        resp.Charset = "utf-8";
        resp.Clear();
        resp.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
        resp.ContentEncoding = System.Text.Encoding.UTF8;

        resp.ContentType = "application/ms-excel";
        string style ="<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>"+ "<style> .table{ font: 9pt Tahoma, Verdana; color: #000000; text-align:center;  background-color:#8ECBE8;  }.table td{text-align:center;height:21px;background-color:#EFF6FF;}.table th{ font: 9pt Tahoma, Verdana; color: #000000; font-weight: bold; background-color: #8ECBEA; height:25px;  text-align:center; padding-left:10px;}</style>";
        resp.Write(style);
        //resp.Write(ExportTable(list));
        resp.Write("<table class='table'><tr><th>" +"手机"+ "</th><th>" + "渠道" + "</th><th>" +"时间" + "</th></tr>");
        //dbVideoList = (List<subShiyongModel>)Session["dbVideoList"];
        foreach (bUFlow.Model.orderhistory model in orderlist)
        {
            resp.Write("<tr><td>" + model.phone + "</td>");
            resp.Write("<td>" + model.qudao + "</td>");
            resp.Write("<td>" + model.tm + "</td>");
            resp.Write("</tr>");
        }
        resp.Write("<table>");

        resp.Flush();
        resp.End();
    }

 

  需要注意的是编码的问题,在输出的时候,最好加上以下语句:

 

复制代码 代码如下:

<meta http-equiv="content-type" content="application/ms-excel; charset=gb2312"/>

您可能感兴趣的文章:

  • .net从服务器下载文件中文名乱码解决方案
  • .NET中文乱码的解决方法分享
  • asp.net Cookie值中文乱码问题解决方法
  • asp.net url传递后地址栏乱码(中文超过两个汉字)
  • ASP.NET中Request.Form中文乱码的解决方法
  • asp.net URL中包含中文参数造成乱码的解决方法
  • asp.net 中文字符串提交乱码的解决方法
  • js与ASP.NET 中文乱码问题
  • asp.net下cookies的丢失和中文乱码
  • .Net获取URL中文参数值的乱码问题解决方法总结

本文由 华域联盟 原创撰写:华域联盟 » asp.net导出Excel显示中文乱码的解决方法

转载请保留出处和原文链接:https://www.cnhackhy.com/48223.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部