双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

导出EXCEL,出现GridView方面的错误

在途中 在途中 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用这一方法导出EXCEL,在TempGrid.RenderControl(oHtmlTextWriter)处出现“类型“GridView”的控件“ctl00_ctl00_MainContent_ContentPlaceHolder1_GridView2”必须放在具有 runat=server 的窗体标记内。”这一错误,试问如何解决?

环境:VS2010 VB GiidView2有20行数据


Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click

GenerateByGridView("导出", GridView2)

End Sub

Public Shared Sub GenerateByGridView(ByVal Typename As String, ByVal TempGrid As GridView)
HttpContext.Current.Response.Clear()
'HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8"
Dim Filename As String = Typename + ".xls"
HttpContext.Current.Response.AppendHeader("Content-Disposition", "online;filename=" + Filename)
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8")
HttpContext.Current.Response.ContentType = "application/ms-excel"
'this.EnableViewState = false;
Dim oStringWriter As New System.IO.StringWriter()
Dim oHtmlTextWriter As New System.Web.UI.HtmlTextWriter(oStringWriter)
TempGrid.RenderControl(oHtmlTextWriter)
HttpContext.Current.Response.Write(oStringWriter.ToString())
HttpContext.Current.Response.End()
End Sub 'GenerateByGridView
...全文
评论 6
收藏 已收藏
分享
返回顶部
在途中
.NET社区 62,274 成员 668,968 内容