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="fal se" in the aspx page settings.
Hope someone can help me..Thanks!
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="fal
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
Here are some links that have the information on exporting a grid to Excel.
http://www.c-sharpcorner.com/uploadfile/dipalchoksi/exportxl_asp2_dc11032006003657am/exportxl_asp2_dc.aspx
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx
Arun
http://www.c-sharpcorner.com/uploadfile/dipalchoksi/exportxl_asp2_dc11032006003657am/exportxl_asp2_dc.aspx
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx
Arun
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.
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
Arun
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try rendering the GridView on screen or check whether there is any data to be rendered or not
Regards,
Chinmay