zoukankan      html  css  js  c++  java
  • 解决导出中文文件名乱码的方法

     

    解决导出中文文件名乱码的方法

     792人阅读 评论(0) 收藏 举报
    /// <summary>
            /// 转换中文excel名称,防止乱码
            /// </summary>
            /// <param name="fileName">中文名称</param>
            /// <returns></returns>
            private static string GetToExcelName(string fileName)
            {
                string UserAgent = HttpContext.Current.Request.ServerVariables["http_user_agent"].ToLower();
                if (UserAgent.IndexOf("firefox") == -1)
                    fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
                return fileName;

            }public enum ExportType {WORD,EXCELL }

            public static void Export(string htmlToExport, string filename,ExportType eType)
            {
               
                filename = HttpUtility.UrlDecode(filename);
                string attachment = string.Empty;
                if (eType.ToString() == "WORD")
                {
                     attachment = "attachment;filename=" + GetToExcelName(filename) + ".doc";
                     System.Web.HttpContext.Current.Response.ContentType = "application/vnd.msword";
                }
                else
                {
                     attachment = "attachment;filename=" + GetToExcelName(filename) + ".doc";
                     System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                }
                System.Web.HttpContext.Current.Response.ClearContent();
                System.Web.HttpContext.Current.Response.AddHeader("content-disposition", attachment);
                System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                //page.Response.ContentEncoding = page.Response.HeaderEncoding;
                

                System.Web.HttpContext.Current.Response.Write(htmlToExport);


                System.Web.HttpContext.Current.Response.End();
            }

  • 相关阅读:
    Django ORM操作
    两张表是一对一的关系为什么不直接都放在一张表里面?
    TCP、UDP 详解
    如何使用Xshell连接虚拟机
    selenium中常见的无法定位元素问题
    python中生产者消费者模式
    Selenium 点击后跳转至新窗口无法定位元素问题(element not interactable)
    linux 网络管理
    Linux文件权限
    linux 用户管理命令
  • 原文地址:https://www.cnblogs.com/jcz1206/p/3517796.html
Copyright © 2011-2022 走看看