Search code examples
asp.netvb.netexcelgridviewexport-to-excel

Export gridview to excel in VB.Net not taking provided filename?


I have the following code to export a gridview to excel and the export works just fine. The issue is that no mater what I do it names the file the name of the webform .xls instead of the name I am providing in the code (Team.xls).

 Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcell.Click
    Dim sw As New StringWriter()
    Dim hw As New System.Web.UI.HtmlTextWriter(sw)
    Dim frm As HtmlForm = New HtmlForm()

    Page.Response.AddHeader("content-disposition", "attachment;Team.xls")
    Page.Response.ContentType = "application/vnd.ms-excel"
    Page.Response.Charset = ""
    Page.EnableViewState = False
    frm.Attributes("runat") = "server"
    Controls.Add(frm)
    frm.Controls.Add(gvTeam)
    frm.RenderControl(hw)
    Response.Write(sw.ToString())
    Response.End()

End Sub

Solution

  • You have forgotten to mention filename=Team.xls

    It should be "attachment;filename=Team.xls" instead of attachment;Team.xls