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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 吹雪

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