如何解决asp.net导出Excel显示中文乱码的问题

发布时间:2021-07-22 17:58:12 作者:chen
来源:亿速云 阅读:175

这篇文章主要介绍“如何解决asp.net导出Excel显示中文乱码的问题”,在日常操作中,相信很多人在如何解决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"/>

到此,关于“如何解决asp.net导出Excel显示中文乱码的问题”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. asp.net怎么解决文本中文乱码的问题
  2. 如何解决java导出excel出现中文乱码问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:Ubuntu中怎么设置网卡IP

下一篇:Linux中进程怎么设置睡眠和唤醒

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》