We help IT Professionals succeed at work.
Get Started

Export Gridview to Excel, get empty excel file.

1,023 Views
Last Modified: 2013-11-27
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
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE