【问题标题】:exporting gridview contents to excel spreadsheet将gridview内容导出到excel电子表格
【发布时间】:2010-02-25 10:32:40
【问题描述】:

我有一个 gridview (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>

【问题讨论】:

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


    【解决方案1】:

    此错误消息后面有一个“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

    【讨论】:

    • 感谢您的帮助。我添加了上面的代码,现在得到:RegisterForEventValidation can only be called during Render();
    • 我通过关闭事件验证解决了这个问题.....通过将 EnableEventValidation ="false" 添加到 @page 指令......这会带来任何潜在的危险吗?
    • @Phil,我假设您只关闭该特定页面的事件验证,您需要确保该页面的回发事件不会被劫持以执行非法或不需要的操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多