C# asp.net 把GridView数据 导出到 Excel

1         Response.Clear();
 2         Response.Buffer  =   true ;
 3         Response.Charset  =   " GB2312 " ;
 4         Response.AppendHeader( " Content-Disposition " " attachment;filename=jtkc.xls " );
 5         Response.ContentEncoding  =  System.Text.Encoding.GetEncoding( " GB2312 " ); // 设置输出流为简体中文
 6         Response.ContentType  =   " application/ms-excel " ; // 设置输出文件类型为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         grvExcel.RenderControl(oHtmlTextWriter);
12         Response.Write(oStringWriter.ToString());
13         Response.End(); 

 

1.这过程中遇到一些问题,刚开始是提示GridView必须放在runat="server"的窗体中,但是已经设置了runat="server"但还是报错,问题解决如下:

只需要重写VerifyRenderingInServerForm方法,将内容清空就可以了。

 

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gt      public   override   void  VerifyRenderingInServerForm(Control control)
   
{
   }

 

 

2。第二个问题是提示:只能在 Render(); 期间呼叫 RegisterForEventValidation

    这个问题比较好解决,只需要在前台页面的@Page指令中加上一个属性:EnableEventValidation="false"

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-566599/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12639172/viewspace-566599/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值