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

推荐订阅源

人人都是产品经理
人人都是产品经理
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
F
Fortinet All Blogs
The Register - Security
The Register - Security
雷峰网
雷峰网
博客园 - 【当耐特】
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
S
Schneier on Security
Latest news
Latest news
S
Securelist
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
NISL@THU
NISL@THU
Cyberwarzone
Cyberwarzone
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
Spread Privacy
Spread Privacy
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
SecWiki News
SecWiki News
N
News and Events Feed by Topic
T
Threatpost
The GitHub Blog
The GitHub Blog
博客园_首页
F
Full Disclosure
爱范儿
爱范儿
The Hacker News
The Hacker News
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Last Week in AI
Last Week in AI
月光博客
月光博客
C
Check Point Blog
G
Google Developers Blog
M
MIT News - Artificial intelligence
博客园 - 司徒正美

博客园 - 特困生

腾讯围脖邀请 office2010 bata 4536 激活问题 windows7 64位 建行捷德UKey无法使用的问题 清除windows7 在任务栏上显示的图标和通知,自定义选项内的图标 Windows7 IIS 7.0 局域网无法访问的解决 - 特困生 c#.net 邮件发送 Maxdos v6 pxe 网刻教程 去掉微软的正版验证 高流量Web2.0网站性能调优的14条准则 Team Foundation但服务器部署 解决visual studio自动调试问题 Visual Studio 2005的各个版本 生成汉字验证码 - 特困生 - 博客园 C#常用函数和方法集汇总 sql2000和sql2005共存问题 安装vs2005sp1补丁注意事项 XMLHttpRequest对象 Web页面折线图控件(可以直接绑定数据) c#绘制曲线图
GridView数据导出到Excel中
特困生 · 2007-04-19 · via 博客园 - 特困生

将GridView中数据导出到Excel中

protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!Page.IsPostBack)
        
{
            BindData();
        }

    }

    
    
private void BindData()
    
{
        
string query = "SELECT * FROM orders";
        SqlConnection conn 
= new SqlConnection("server=.;database=northwind;uid=sa;pwd=sa");
        SqlDataAdapter ad 
= new SqlDataAdapter(query, conn);
        DataSet ds 
= new DataSet();
        ad.Fill(ds);
        GridView1.DataSource 
= ds.Tables[0];
        GridView1.DataBind();

    }



    
public override void VerifyRenderingInServerForm(Control control)
    
{

        
    }


    
protected void Button2_Click(object sender, EventArgs e)
    
{
        Response.Clear();

        Response.AddHeader(
"content-disposition","attachment;filename=FileName.xls");

        Response.Charset 
= "gb2312";
        Response.ContentType 
= "application/vnd.xls";

        System.IO.StringWriter stringWrite 
= new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite 
= new HtmlTextWriter(stringWrite);


        GridView1.AllowPaging 
= false;
        BindData();
        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();

        GridView1.AllowPaging 
= true;
        BindData();

    }

导出成功!
注意如果数据源是NorthWind中的Products表(原因不清楚,应该还有其他的数据源也会出现这种情况)
会出现异常
只能在执行 Render() 的过程中调用 RegisterForEventValidation;有两种方法可以解决这个问题:
1.修改web.config(不推荐)<pages enableEventValidation ="false" ></pages>
2.直接在导出Execl的页面修改

<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true"

 CodeFile
="ExportGridView.aspx.cs" Inherits="ExportGridView" 
%>