Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I would like to export the data from GridView to Excel 2007 file (xlsx). Here is my code. However,I get an error message: The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?

Help.:confused:

VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Response.Clear()
    Response.Buffer = True
    Response.Charset = ""
    Response.AppendHeader("Content-Disposition", "attachment;filename=statistic.xlsx")
    Response.ContentEncoding = System.Text.Encoding.UTF7
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Dim oStringWriter As System.IO.StringWriter = New System.IO.StringWriter
    Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(oStringWriter)
    Me.GridView1.RenderControl(oHtmlTextWriter)
    Response.Output.Write(oStringWriter.ToString)
    Response.Flush()
    Response.End()
End Sub
Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As System.Web.UI.Control)

End Sub
Posted

Hello lizhunji,

Excel 2007 does not read pure html like 2003-2005 versions I'm afraid, you have two options that I can think of right now and I'm certain many others.

You can write the gridview as an excel 2005 spreadsheet with the file extension xls and when you open that up in excel 2007 you get a warning message "The file you are trying to open 'FileName.xls' is in a different format than the specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?" That is one long A** message and very unprofessional, but! it works.

Or you can down the method of writing the file as a CSV and changing the extension to xlsx which is excel 2007, sure is annoying! But Microsoft wanted to downgrade functionality I'm afraid (Not hating Microsoft!)

I have no code for the CSV method as yet, but if you want to do the first method then your code is almost already there. Try adapt this.

VB
Response.Clear()

       ' Set the type and filename
       Response.AddHeader("content-disposition", "attachment;filename=FileActivity" & "_" & Format(Date.Now, "dd/MM/yyyy_HHmm") & ".xls")
       Response.Charset = ""
       Response.ContentType = "application/vnd.xls"

       ' Add the HTML from the GridView to a StringWriter so we can write it out later
       Dim sw As System.IO.StringWriter = New System.IO.StringWriter
       Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(sw)
       grdActivity.RenderControl(hw)

       ' Write out the data
       Response.Write(sw.ToString)
       Response.End()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900