如何将GRIDVIEW导出到EXCEL
Sub ExportExcel(ByVal send As Object, ByVal e As EventArgs)
'导出EXCEL
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
Dim hw As System.Web.UI.HtmlTextWriter = New ystem.Web.UI.HtmlTextWriter(sw)
Dim nCur As Integer = GridView1.PageIndex
Dim nSize As Integer = GridView1.PageSize
GridView1.AllowPaging = False
GridView1.DataBind()
GridView1.RenderControl(hw) '这一句出错!!!
GridView1.AllowPaging = True
GridView1.PageIndex = nCur
GridView1.PageSize = nSize
GridView1.DataBind()
Response.Write(sw.ToString())
Response.End()
End Sub
“/Asp.netBase”应用程序中的服务器错误。
——————————————————————————–
类型“GridView”的控件“ctl00_ContentPlaceHolder1_GridView1”必须放在具有 runat=server 的窗体标记内。
解决方法: 加入
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
End Sub