web页面导出到Excel乱码解决
2011-02-25 11:26
183 查看
引言:
前几天 在做web项目的时候 需要导出页面上的数据 到Excel里面
但有的时候出现乱码(有de时候不出现 很奇怪)
原来的代码是这样的:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//.Unicode;//.UTF8;//
HttpContext.Current.Response.ContentType = "xls"; //"application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
mygridview.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
如何解决?
修改为下面的代码 问题就解决了
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type; content=text/html;charset=gb2312>");
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
mygridview.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
仔细比较一下 就是
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type; content=text/html;charset=gb2312>");
这句话起的作用 (这句话的作用是声明该网页使用gb2312进行编码)
原来是 HttpContext.Current.Response.Charset = "GB2312";
我以为有这句话 就可以 看来不行
还是 使用 <meta http-equiv=Content-Type; content=text/html;charset=gb2312> 比较保险
前几天 在做web项目的时候 需要导出页面上的数据 到Excel里面
但有的时候出现乱码(有de时候不出现 很奇怪)
原来的代码是这样的:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment;filename=FileName.xls");
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//.Unicode;//.UTF8;//
HttpContext.Current.Response.ContentType = "xls"; //"application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
mygridview.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
如何解决?
修改为下面的代码 问题就解决了
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type; content=text/html;charset=gb2312>");
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
mygridview.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
仔细比较一下 就是
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type; content=text/html;charset=gb2312>");
这句话起的作用 (这句话的作用是声明该网页使用gb2312进行编码)
原来是 HttpContext.Current.Response.Charset = "GB2312";
我以为有这句话 就可以 看来不行
还是 使用 <meta http-equiv=Content-Type; content=text/html;charset=gb2312> 比较保险
相关文章推荐
- 导出Execl 系列: web页面导出到Excel乱码解决
- web页面导出到Excel乱码解决
- 设计能够导出所有记录到excel的分页(使用AspNetPager)页面,并解决excel文件名中文乱码问题
- Java在web页面上中文URL乱码解决
- PHP excel导出乱码问题解决办法
- 使用JavaScript将Web页面内容导出到Word、Excel
- 基于PHP导出Excel的小经验 完美解决乱码问题
- phpexcel导出excel中文乱码问题解决
- 使用UltraWebGridExcelExporter控件导出分页Grid所有数据的简单解决办法
- WEB页面导出为EXCEL、word文档的方法(完整版)
- Java中Excel导出时文件名乱码问题的解决
- DataGrid数据导出excel文件,有时出现乱码解决方法。
- Web页面的数据导出excel时的格式问题(长数字显示为科学计数法格式等)
- WEB页面导出为WORD,EXCEL文档的方法
- python解决导出excel文件时中文文件名乱码
- jsp导出excel 解决文件名中文乱码
- 将GridView中的数据导出到Excel中下载并且解决乱码的问题
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- Web页面的数据导出excel时的格式问题
- 导出Excel文档 解决导出Excel文档显示乱码 在C#桌面程序导出Excel文档