We help IT Professionals succeed at work.

Export Gridview to Excel, get empty excel file.

Hi,

I have a very simple gridview that i want to export to excel.  I have a button that the user will click to export the gridview content to excel.

The code is pretty standard (see in code section), and i've successfully done it for many pages before.  Somehow for this one, i could not get it to work.  It downloads an excel file, but the sheet is empty.

The server is running IIS 6 on Windows Server 2003.  Is there any server settings i need to change to make this work? My code is attached below. I also have set EnableEventValidation="false" in the aspx page settings.

Hope someone can help me..Thanks!
'Export button code
Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Dim filename As String
        filename = "MMEAHistory"
        ExportToExcel(filename)
End Sub

'Export Code
Public Sub ExportToExcel(ByVal filename As String)
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=" & filename & ".xls")
        Response.Charset = ""
        Response.ContentType = "application/vnd.xls"

        Dim sw As System.IO.StringWriter = New System.IO.StringWriter
        Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
        GridView1.RenderControl(hw)

        Response.Write(sw.ToString)
        Response.End()
End Sub

Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub

Open in new window

Comment
Watch Question

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi GCCTECHIE,
Try rendering the GridView on screen or check whether there is any data to be rendered or not
Regards,
Chinmay


Top Expert 2009

Commented:

Author

Commented:
Hi,

I'm so sorry. I'd like to retract the question. The problem occured because the gridview is set to visible = false on load to prevent thousands of rows of data to be loaded initially.

I resolved it by making the gridview visible = true in the first line of the export button code.
Top Expert 2009

Commented:
Please don't retract. Just post the solution and select your post as the answer to the question. This will help others who stumble upon such issues.

Arun
ok sure..the code is as follows for the Export button.

The ExportToExcel method remains the same.

Previously, i kept getting empty excel files because when the user clicks the Export button, the page reloads and the gridview becomes invisible.
'gridview set to invisible to prevent thousands of rows being displayed in the beginning.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.Visible = False
End Sub

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
        GridView1.Visible = True 'added this line to fix the problem
        Dim filename As String
        filename = "MMEAHistory"
        ExportToExcel(filename)
End Sub

Open in new window