导出到Excel方法:

        <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">void</span> ExportToExcel(GridView gridView)
{
gridView.AllowPaging = <span style="color: rgb(0, 0, 255);">false</span>;<span style="color: rgb(0, 128, 0);">//禁用分页,将数据全部导出到EXCEL</span> Response.Clear();
Response.Buffer = <span style="color: rgb(0, 0, 255);">true</span>;
Response.Charset = "<span style="color: rgb(139, 0, 0);">gb2312</span>";
Response.AddHeader("<span style="color: rgb(139, 0, 0);">Content-Disposition</span>", "<span style="color: rgb(139, 0, 0);">attachment; filename=</span>"
+ System.Web.HttpUtility.UrlEncode(DateTime.Now.ToLongDateString()+DateTime.Now.ToLongTimeString(), System.Text.Encoding.UTF8) + "<span style="color: rgb(139, 0, 0);">.xls</span>"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("<span style="color: rgb(139, 0, 0);">gb2312</span>");<span style="color: rgb(0, 128, 0);">//设置输出流为简体中文</span>
Response.Write("");
Response.ContentType = "<span style="color: rgb(139, 0, 0);">application/vnd.ms-excel</span>";<span style="color: rgb(0, 128, 0);">//设置输出文件类型为excel文件。 </span>
<span style="color: rgb(0, 0, 255);">this</span>.EnableViewState = <span style="color: rgb(0, 0, 255);">false</span>; System.Globalization.CultureInfo myCItrad = <span style="color: rgb(0, 0, 255);">new</span> System.Globalization.CultureInfo("<span style="color: rgb(139, 0, 0);">ZH-CN</span>", <span style="color: rgb(0, 0, 255);">true</span>);
System.IO.StringWriter oStringWriter = <span style="color: rgb(0, 0, 255);">new</span> System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = <span style="color: rgb(0, 0, 255);">new</span> System.Web.UI.HtmlTextWriter(oStringWriter); <span style="color: rgb(0, 128, 0);">//这里需要重新绑定数据源</span>
<span style="color: rgb(0, 0, 255);">this</span>.BindgridView(); <span style="color: rgb(0, 128, 0);">//绑定数据源的函数</span> gridView.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}

如上代码如果处理一般的GridView导出应该是没有问题的,但是如果GridView的AutoGenerateDeleteButton,AutoGenerateEditButton,AutoGenerateSelectButton有的设置为True了,或者GridView中有HyperLinkField类型的字段,那么就会抛出形如“类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常!

那么他的解决方案是:对WebForm窗体的VerifyRenderingInServerForm方法进行Override! 

代码如下:

<span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">void</span> VerifyRenderingInServerForm(Control control)
{
<span style="color: rgb(0, 128, 0);">//OverRide 为了使导出成Excel可行!</span>
}

处理乱码的代码:

           Response.ContentEncoding = System.Text.Encoding.GetEncoding("<span style="color: rgb(139, 0, 0);">gb2312</span>");<span style="color: rgb(0, 128, 0);">//设置输出流为简体中文</span>
Response.Write("");

从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常的更多相关文章

  1. 错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 .

    错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 在做导出数据到EXCEL程序中,出现了错误提示:类型“GridView”的控件“Gr ...

  2. 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。

    错误的写法:  if (this.GridView1.Rows.Count > 0)         {             string style = @"<style& ...

  3. 类型“GridView”的控件必须放在具有 runat=server 的窗体标记内?

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

  4. GridView”的控件 必须放在具有 runat=server 的窗体标记内 “错误提示”

    在做导出数据到EXCEL程序中,出现了错误提示:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记 解决办法  重写 VerifyRendering ...

  5. java代码导出数据到Excel、js导出数据到Excel(三)

     jsp内容忽略,仅写个出发按钮:          <button style="width: 100px" onclick="expertExcel()&quo ...

  6. 使用PHP导入Excel和导出数据为Excel文件

    有时需要将Excel表格的数据导入到mysql数据库中,我们使用PHP的一个开源项目PHP-ExcelReader可以轻松实现Excel的导入.另外将mysql数据导出为Excel与本站上篇文章中导出 ...

  7. java Servlet导出数据到Excel文件

    package com.lbc.excel.servlet; import java.io.IOException; import java.util.ArrayList; import java.u ...

  8. CodeIgniterCodeigniter+PHPExcel导出数据到Excel文件

    解压压缩包里的Classes文件夹中的内容到application\libraries\目录下,目录结构如下:--application\libraries\PHPExcel.php--applica ...

  9. 【转】asp.net导出数据到Excel的三种方法

    来源:http://www.cnblogs.com/lishengpeng1982/archive/2008/04/03/1135490.html 原文出处:http://blog.csdn.net/ ...

随机推荐

  1. 【iOS系列】-A server with the specified hostname could not be found.问题解决

    [iOS系列]-A server with the specified hostname could not be found.问题解决 Reveal 在iOS开发中可以方便查看界面的布局,较为方便的 ...

  2. win系统下启动linux上的kafka集群及使用

    一.首先在win系统下C:\Windows\System32\drivers\etc文件夹中hosts文件加入例如以下内容: 10.61.6.167 slaves1 10.61.6.168 slave ...

  3. MD5加密解密帮助类

    using System; using System.Security.Cryptography; using System.Text; namespace Maticsoft.DBUtility { ...

  4. Num 36 : ZOJ 2100 [ 深度优先搜索算法 ] [ 回溯 ]

    该题是用回溯法来解决的题: 题目: Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and fa ...

  5. iOS开发——高级篇——多线程GCD死锁

    面试题 请问以下代码打印结果: - (void)interview01 { // 以下代码是在主线程执行的 NSLog(@"执行任务1"); dispatch_queue_t qu ...

  6. YTU 1098: The 3n + 1 problem

    1098: The 3n + 1 problem 时间限制: 1 Sec  内存限制: 64 MB 提交: 368  解决: 148 题目描述 Consider the following algor ...

  7. C语言-1.static 和 extern使用,2.文件,3.数据块读写

    1.static 和 extern使用, 1)修饰局部变量 static修饰局部变量特点:延长局部变量的生命周期 ,static修饰的局部变量只会被执行一次 extern不能修饰局部变量 2)修饰全局 ...

  8. 并不对劲的Loj6031:「雅礼集训 2017 Day1」字符串

    题目传送门:-> 看到题目的第一反应当然是暴力:对于串s建后缀自动机,每次询问中,求w对应的子串在s的SAM中的right集合.O(qmk)听上去显然过不了. 数据范围有个∑w<=1e5, ...

  9. Python基础第十天

    一.内容

  10. hdu3709 (平衡数) 数位DP

    Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...