引言:
前几天 在做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("");
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("");
这句话起的作用 (这句话的作用是声明该网页使用gb2312进行编码)
原来是 HttpContext.Current.Response.Charset = "GB2312";
我以为有这句话 就可以 看来不行
还是 使用 比较保险

web页面导出到Excel乱码解决的更多相关文章

  1. 【MVC】 非常简单的页面导出 WORD, EXCEL方法

    [MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...

  2. 集群重启后启动ambari-server访问Web页面无法启动集群解决

    集群重启后启动ambari-server访问Web页面无法启动集群解决 使用ambari部署的集群重新启动后,必须手动重启ambari-server和所有集群主机上的ambari-agent. amb ...

  3. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  4. (转)WEB页面导出为Word文档后分页&横向打印的方法

    <html>    <HEAD>        <title>WEB页面导出为Word文档后分页&横向打印的方法 </title>    < ...

  5. 使用python在WEB页面上生成EXCEL文件

    来自:http://blog.sina.com.cn/s/blog_5d18f85f0101bxo7.html 近日写的一个程序需要在WEB服务器上生成EXCEL文件供用户下载,研究了一下找到了以下比 ...

  6. mysql导入导出数据中文乱码解决方法小结

    linux系统中 linux默认的是utf8编码,而windows是gbk编码,所以会出现上面的乱码问题. 解决mysql导入导出数据乱码问题 首先要做的是要确定你导出数据的编码格式,使用mysqld ...

  7. javascript通过url向jsp页面传递中文参数乱码解决方法

    解决方法:在传递参数前将中文参数进行两次编码,jsp页面获取参数后对中文参数进行一次解码,中文参数就不会变为乱码了! 参考例子: <%@ page language="java&quo ...

  8. html页面导出为excel表格

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  9. LoadRunner web请求和响应中文乱码解决办法

    先来认识下lr_convert_string_encoding: int lr_convert_string_encoding( const char *sourceString, const cha ...

随机推荐

  1. sql server deadlock problem

    https://www.red-gate.com/simple-talk/sql/learn-sql-server/how-to-track-down-deadlocks-using-sql-serv ...

  2. BZOJ4311:向量

    题意:要求支持三个操作,加入删除一个向量,询问当前向量与给定向量的最大值. 题解:线段树时间分治,每个区间做一个凸包,查询的时候到对应区间的凸包上三分. (话说我这个可能有点问题,三分那一块R-L&g ...

  3. Spark 属性配置

    1.Spark1.x 属性配置方式 Spark属性提供了大部分应用程序的控制项,并且可以单独为每个应用程序进行配置. 在Spark1.0.0提供了3种方式的属性配置: SparkConf方式 Spar ...

  4. 在Windows下使用adb logcat grep

    在Windows下使用adb logcat  grep 会提示 因为grep 为Linux命令,所以不能使用.怎么办呢? 这时候可以用到babun 下载地址:http://babun.github.i ...

  5. Python之爬虫总结

    一.爬虫之requests a.介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3)     b.注意:re ...

  6. IE 内使用ActiveX,写注册表被重定向到如下注册表

    IE 内使用ActiveX,写注册表被重定向到如下注册表,所以使用注册表做标记要注意下,目前还没找为什么会这样 HKEY_CURRENT_USER\Software\Microsoft\Interne ...

  7. MVC3 ajax功能

    微软mvc3框架的项目使用微软自带的ajax 必须引用下面 <script src="/Scripts/jquery.unobtrusive-ajax.js" type=&q ...

  8. lr中检查点的使用web_find()和web_reg_find()的区别

    web_find()和web_reg_find()的区别:1. 这两个函数函数类型不同,web_find()是普通函数,web_reg_find()是注册函数;2. VU run time设置中的 “ ...

  9. fatal: Could not read from remote repository.

    fatal: Could not read from remote repository. Please make sure you have the correct access rightsand ...

  10. Idea_学习_10_Idea远程debug

    一.前言 二.远程debug 1.在远程机器启动java调试模式. 需要在启动时添加如下jvm参数,来以java调试模式运行项目. java -Xdebug -Xrunjdwp:server=y,tr ...