ASP.Net Gridview to Excel

To export the content of gridview to an excel file you can use the following syntax on you button

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=report.xls")
        Dim mystringwriter As New IO.StringWriter()
        Dim myhtmltextwriter As New HtmlTextWriter(mystringwriter)
        GridView1.RenderControl(myhtmltextwriter)
        Response.Write(mystringwriter.ToString())
        Response.End()
End Sub

In the code behind you also need to override the sub VerifyRenderingInServerForm, just comment the code inside the sub and your good to go.

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)
        'MyBase.VerifyRenderingInServerForm(control)

End Sub

Comments

Popular Posts