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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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"/>

脚本宝典总结

以上是脚本宝典为你收集整理的asp.net导出Excel显示中文乱码的解决方法全部内容,希望文章能够帮你解决asp.net导出Excel显示中文乱码的解决方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:asp.net