GridView导出Excel

     我这种懒人太幸福了,网上有许多GridView导出到Excel的优秀代码.以往直接Google之.用了这么久,今天自己写的时候动不动就卡在某一个Exception上抓狂不已.晚上静下心来整理了下今天碰到的问题,跟各位网友分享一下:)

Step 1: 

   似乎用的最多的就是使用Control.RenderControl方法来导出,我也采用了这个方法.代码如下:

        GridView1.AllowPaging = false;//取消分页,使GridView显示全部数据.
        GridView1.DataBind();//重新绑定.
        Response.Clear();
        Response.AppendHeader("Content-Disposition""attachment;filename= Export.xls");
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件.
        this.EnableViewState = false;
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        GridView1.AllowPaging = true;

 

 

 

  好了,开始报错了:

 

扯淡了吧? 怎么可能呢?<form id="form1" runat="server">好好的啊.Google之:

override页面的VerifyRenderingInServerForm(Control control)方法: VerifyRenderingInServerForm(Control control){}.

P.S:这里说一下,在Visual Studio 2003中,此处会直接在报System.Web.HttpUnhandledException的错误.还没Visual Studio 2003中还没有异常的详细信息,我是加上了Try-Catch块才发现的错误.似乎在Visual Studio 2003中许多的错误都会直接报HttpUnhandledException,Try-Catch后才能看到错误.希望能给大家一些提示. 

不知道为什么.先加上之后测试一下.

Step 2:

又报错了: 

 

饿...继续Google之:

有两种方法可以解决以上问题:

1.修改web.config(不推荐)<pages enableEventValidation ="false" ></pages>
2.直接在导出Execl的页面修改:
<%@ Page Language="C#" EnableEventValidation = "false" AutoEventWireup="true" CodeFile="ExportGridView.aspx.cs" Inherits="ExportGridView" %>

 MSND对此的解释:必须位于 <form runat=server> 标记中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息。发送回或依赖于注册的脚本块的控件应该在 Control.Render 方法的重写中调用此方法。呈现服务器窗体元素的方式不同的页可以重写此方法以在不同的条件下引发异常。

如果回发或使用客户端脚本的服务器控件没有包含在 HtmlForm 服务器控件 (<form runat="server">) 标记中,它们将无法正常工作。这些控件可以在呈现时调用该方法,以在它们没有包含在 HtmlForm 控件中时提供明确的错误信息。(引用自:DotNetEden的日志:"控件必须放在具有 runat=server 的窗体标记内"错误的解决方法) 

OK,继续测试.

Step 3:

两个问题:

1.文件名称乱码;

2.不光导出GridView,整个页面都导出了.

好吧,继续Google.伟大的Google再次告诉我解决方法:

乱码嘛,好解决,编码的问题.加上以下代码:

 导出到Excel,解决Excel文件名乱码问题

        GridView1.AllowPaging = false;//取消分页,使GridView显示全部数据.
        GridView1.DataBind();//重新绑定.
        Response.Clear();
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition""attachment;filename= " + Server.UrlEncode("导出.xls"));
        
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件.
        this.EnableViewState = false;
        
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN"true);
        System.IO.StringWriter stringWrite = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        GridView1.AllowPaging = true;

 

注意红色的代码.这样就可以解决由于中文名称引起的乱码问题(设置编码为"GB2312").

至于导出的时候把页面所有的东西都导出的问题,我很无语,仔细检查代码之后发现竟然没有写 Response.End()=.=鄙视一下自己.哈哈

 代码

        GridView1.AllowPaging = false;//取消分页,使GridView显示全部数据.
        GridView1.DataBind();//重新绑定.
        Response.Clear();
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition""attachment;filename= " + Server.UrlEncode("导出.xls"));
        
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件.
        this.EnableViewState = false;
        
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN"true);
        System.IO.StringWriter stringWrite = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
        GridView1.AllowPaging = true;

Step 4:

 到此可算是导出了,但,仔细看Excel中的内容又发现一个问题:

 Excel中,类似"002432" 的内容都变成了"2432".日期格式也跟GridView中的不一样.Google之:原来是Excel对列的格式做的自动设置搞的鬼.那么再加一行代码就可以了:

代码

        GridView1.AllowPaging = false;//取消分页,使GridView显示全部数据.
        GridView1.DataBind();//重新绑定.
        Response.Clear();
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition""attachment;filename= " + Server.UrlEncode("导出.xls"));
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件.
        this.EnableViewState = false;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN"true);
        System.IO.StringWriter stringWrite = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.Write(@"<style> .text { mso-number-format:\@; } </script> ");
        Response.End();
        GridView1.AllowPaging = true;

 

 

 测试,终于没有什么问题了.

 

 其实这这些只是很简单的操作,大家看了一定见笑了.关于GridView导出到Excel,这里有更好的解决方案:

 扩展 GridView 控件 - 支持 Excel 及 Word 汇出

小菜鸟一个,请各位网友指点:)

posted @ 2010-01-12 23:02  Ztmdsbt  阅读(2574)  评论(8编辑  收藏  举报