您的位置:首页 > 其它

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

2011-09-07 10:20 549 查看
/// <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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐