解决导出中文文件名乱码的方法
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();
}
/// 转换中文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();
}
相关文章推荐
- Gridview导出为Excel的时候,中文文件名乱码,解决方法
- 解决导出中文文件名乱码的方法
- mysql导入导出数据中文乱码解决方法小结
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- Mysql 导入导出csv 中文乱码问题的解决方法
- php上传文件中文文件名乱码的解决方法
- 解决在IE下使用PHPExcel导出时的文件名中文乱码问题
- 解决swfupload上传控件文件名中文乱码问题 三种方法
- mysql导入导出数据中文乱码解决方法小结
- linux命令行下中文文件名乱码的解决方法
- 跨浏览器PHP下载文件名中的中文乱码问题解决方法
- 设计能够导出所有记录到excel的分页(使用AspNetPager)页面,并解决excel文件名中文乱码问题
- 彻底解决Moodle上传文件中文文件名乱码解决方法
- h5ai v0.29中文文件名、文件、url乱码解决方法
- Xftp连接主机文件名显示中文乱码且不能下载的本地解决方法
- Mysql 导入导出csv 中文乱码问题的解决方法
- 关于RCP项目使用UTF-8编码,导出后中文乱码的解决方法。
- php上传中文文件名乱码解决方法
- C# 中文数据导出CSV出现乱码解决方法
- 实战mysql导出中文乱码及phpmyadmin导入中文乱码的解决方法