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

推荐订阅源

A
Arctic Wolf
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Google Online Security Blog
Google Online Security Blog
Help Net Security
Help Net Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
The Exploit Database - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security Affairs
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
月光博客
月光博客
博客园 - Franky
The GitHub Blog
The GitHub Blog
O
OpenAI News
The Cloudflare Blog
Google DeepMind News
Google DeepMind News
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
爱范儿
爱范儿
S
Secure Thoughts
T
The Blog of Author Tim Ferriss
SecWiki News
SecWiki News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
Hacker News: Ask HN
Hacker News: Ask HN
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
人人都是产品经理
人人都是产品经理
腾讯CDC
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
The Last Watchdog
The Last Watchdog
博客园 - 叶小钗
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
PCI Perspectives
PCI Perspectives
罗磊的独立博客
有赞技术团队
有赞技术团队
B
Blog RSS Feed
L
LINUX DO - 最新话题

博客园 - 侯垒

中转站余额为什么掉得快?我拆了一次 AI 编程任务的真实消耗 我让 Claude 写了一个贪吃蛇游戏,然后用 ccglass 看清它发给模型的真实请求 AI 编程 Agent 不是黑箱了:用 ccglass 看清 Claude Code 和 Codex 的真实请求 软件工程--软件过程模型 UML 类图 WebService基于SoapHeader实现安全认证 (转)深入理解Javascript闭包(closure) 数据库中使用自增量字段与Guid字段作主键的性能对比(补充篇) SQL Server 查询处理中的各个阶段 敏捷软件开发--敏捷宣言 asp.net抛出System.Data.OleDb.OleDbException:未指定的错误 架构师 通用的数据库操作类(支持多种数据库) 数据库中使用自增量字段与Guid字段作主键的性能对比 设计模式----建造者模式(Builder Pattern) 大学生联盟开发团队需要我们共同的努力 - 侯垒 - 博客园 SQL 数据库操作类 只启动一个窗体,如果再次启动则激活该窗体 如何将自己的代码自动添加版权信息的及其扩展 如何将自己的代码自动添加版权信息 轻松学习适配器模式(Adapter Pattern) 设计模式-----桥接模式(Bridge Pattern)
将GridView中的数据导出到Excel中下载并且解决乱码的问题
侯垒 · 2008-12-01 · via 博客园 - 侯垒

将GridView中的数据导出到Excel中下载并且解决乱码的问题

在软件开发过程中我们经常将GridView中的数据导出到Excel中供客户端下载,以下提供一种解决供参考,欢迎大家讨论并提出其中的不足,以达到相互学习共同提高.

步骤1:

添加如下事件到GridView所在的页面 

//下载GridView,添加如下事件;
    public override void VerifyRenderingInServerForm(Control control)
   
{

    }

步骤2:

调用的方法如下 

 /// </summary>
        
/// <param name="g">要下载的GridView</param>
        
/// <param name="fileName">文件名;</param>
        
/// <param name="table">数据源,为了解决分页问题</param>

        public static void DownLoad(GridView g,string fileName,DataTable table)
        
{
            g.DataSource 
= table;
            g.AllowPaging 
= false;
            g.DataBind();
            fileName 
= HttpUtility.UrlEncode(fileName + ".xls", Encoding.GetEncoding("utf-8"));
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader(
"content-disposition""attachment;filename=" + fileName);
            HttpContext.Current.Response.Write(
"<meta   http-equiv=Content-Type   content=text/html;charset=utf-8>");   

            
//Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");//即可导成word;
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentType 
= "application/vnd.xls";
            System.IO.StringWriter stringWrite 
= new System.IO.StringWriter();
            ShowGridViewHeader.OnlyShowGridViewHeader(g);
            System.Web.UI.HtmlTextWriter htmlWrite 
= new HtmlTextWriter(stringWrite);
            g.RenderControl(htmlWrite);
            
HttpContext.Current.Response.Write(stringWrite.ToString());
            HttpContext.Current.Response.End();
       }

通过以上两个步骤就可以实现对GridView中数据的导出与下载,如果你在下载后没有出现乱码现象,那么真的很恭喜你.对于数据的下载有的计算机会出现乱码现象,而有的计算机不会.解决方案如下:

解决方法

1.保存的文件名不能使用中文,如果想使用中文请对其进行编码;

2.配置web.config代码如下

<system.web>
    
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
</system.web>