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

导出到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("");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值