将gridview内容导出到Excel电子表格

时间:2010-02-25 10:32:40

标签: asp.net vb.net excel gridview export

我有一个gridvidew(GV2)。我希望用户能够将此gridview的内容导出到Excel电子表格以进行离线处理。

这是我的子程序:

Protected Sub ExcelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExcelButton.Click
        Response.ContentType = "application/vnd.ms-excel"
        Response.Charset = ""
        Me.EnableViewState = False
        Dim stringWriter As New System.IO.StringWriter()
        Dim htmlWriter As New System.Web.UI.HtmlTextWriter(stringWriter)
        GV2.RenderControl(htmlWriter)
        Response.Write(stringWriter.ToString())
        Response.End()
    End Sub

单击ExcelButton时出现错误消息:

“GridView”类型的控件“GV2”必须放在带有runat = server的表单标记内。

控件GV2实际上在里面:

 <form id="form1" runat="server"></form>

1 个答案:

答案 0 :(得分:2)

此错误消息后面有一个“story”。我会直接找到其中一个解决方案。

将此添加到ASPX文件的代码隐藏:

Public Overrides Sub VerifyRenderingInServerForm(control As Control)

End Sub

更多信息:

How to export GridView to Word using ASP.NET 2.0 and VB
CodeSnip: Exporting GridView to Excel