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

2023-05-26,,

导出到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 的窗体标记内。”的异常的相关教程结束。

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

下载本文的Word格式文档,以方便收藏与打印。