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

推荐订阅源

Forbes - Security
Forbes - Security
A
Arctic Wolf
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
A
About on SuperTechFans
P
Palo Alto Networks Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
D
DataBreaches.Net
Cyberwarzone
Cyberwarzone
T
Tor Project blog
IT之家
IT之家
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
N
News and Events Feed by Topic
The Cloudflare Blog
S
Schneier on Security
P
Privacy & Cybersecurity Law Blog
T
The Blog of Author Tim Ferriss
Recorded Future
Recorded Future
Last Week in AI
Last Week in AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
LangChain Blog
I
InfoQ
F
Full Disclosure
The Register - Security
The Register - Security
阮一峰的网络日志
阮一峰的网络日志
H
Hacker News: Front Page
V
V2EX

博客园 - 吹雪

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了。