惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
N
News and Events Feed by Topic
D
DataBreaches.Net
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
博客园_首页
Microsoft Azure Blog
Microsoft Azure Blog
Y
Y Combinator Blog
博客园 - Franky
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
A
About on SuperTechFans
I
InfoQ
S
Securelist
Last Week in AI
Last Week in AI
S
Schneier on Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hacker News: Ask HN
Hacker News: Ask HN
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
腾讯CDC
大猫的无限游戏
大猫的无限游戏
S
Security @ Cisco Blogs
博客园 - 三生石上(FineUI控件)
Simon Willison's Weblog
Simon Willison's Weblog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
美团技术团队
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
罗磊的独立博客
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
S
Secure Thoughts
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Latest news
Latest news
Recent Announcements
Recent Announcements
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LINUX DO - 热门话题
Security Latest
Security Latest
TaoSecurity Blog
TaoSecurity Blog
Cyberwarzone
Cyberwarzone
有赞技术团队
有赞技术团队

博客园 - 吹雪

Web开发的一些小细节 - 吹雪 - 博客园 如何卸载WSS3.0自带的SSEE Database 版本控管的重要性 自定义 IIS 6 错误信息吗? VS2003 Bug "刷新项目失败,无法从Server获取目录信息" - 吹雪 SOA的一些基本理解 关于SOA的基本介绍。来源于IBM DeveloperWorld 关于数据库安全性的 ITPortal与SOA 用javascript动态调整iframe高度 (ZT) - 吹雪 - 博客园 关于网站兼容性问题的研究 SubVersion还是蛮好用的 基于web的版本控制工具 Visual Studio 2005要等到2006年了 呵呵 昨天刚刚申请到Wallop今天就可以邀请4个名额和4个Gmail名额 今天从其他Blog上面看到的一些对自己有价值的功能收藏先,空了慢慢研究 推荐一个IE的下载插件(下载DNN的时候能到150K左右) [寻书]请问谁有《Pragmatic Unit Testing in C# with NUnit 》的电子版,请发给我一下,谢谢! 免费邮箱的大小
文件流形式导出为Excel的文件编码问题
吹雪 · 2005-06-27 · via 博客园 - 吹雪

今天作项目的时候遇到一个问题:
   DataGrid内容导出为Excel文件有可能乱码。

描述如下:
      简体中文系统中web.config文件设置了<globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-cn" />

    系统里面有a.aspx提供了一个Datagrid,和Button,点击Button的时候将Datagrid的内容导出为Excel。当a.aspx被不同页面调用的时候导出的excel文件有可能会乱码,但又不是全部情况都乱码。调试了半天也没有发现问题。

 1string filename = "" ;
 2   filename = DateTime.Now.ToString("yyyy-MM-dd"+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
 3   Response.Clear(); 
 4   Response.Buffer= true
 5   Response.AppendHeader("Content-Disposition","attachment;filename=" + filename + ".xls"); 
 6   Response.ContentType = "application/ms-excel";
 7   this.EnableViewState = false;    
 8   System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-cn",true);
 9   System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
10   System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
11   DataGrid_BudgetUsedList.RenderControl(oHtmlTextWriter); 
12   Response.Write(oStringWriter.ToString());
13   Response.End();  
14

最后发现是编码问题

 1string filename = "" ;
 2   filename = DateTime.Now.ToString("yyyy-MM-dd"+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();
 3   Response.Clear(); 
 4   Response.Buffer= true
 5   Response.AppendHeader("Content-Disposition","attachment;filename=" + filename + ".xls"); 
 6   Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8"); //更改为Utf-8编码后正常
 7   Response.ContentType = "application/ms-excel";
 8   this.EnableViewState = false;    
 9   System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("zh-cn",true);
10   System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 
11   System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
12   DataGrid_BudgetUsedList.RenderControl(oHtmlTextWriter); 
13   Response.Write(oStringWriter.ToString());
14   Response.End();  
15

设置ContentEncoding为UTF-8之后显示正常。
唉~受习惯思维影响,习惯性认为简体中文系统就应该是gb2312。有时候换一下思维问题也就解决了。

ps:系统使用Utf-8编码应该是最佳方案,但是有时候受一些东西的影响也只能使用GB2312了。