解決導出中文文件名亂碼的方法

/// <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();
        }


發佈了40 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章