Link to home
Start Free TrialLog in
Avatar of GCCTECHIE
GCCTECHIE

asked on

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

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

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


Avatar of GCCTECHIE
GCCTECHIE

ASKER

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.
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
ASKER CERTIFIED SOLUTION
Avatar of GCCTECHIE
GCCTECHIE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial