Last post May 11, 2017 05:43 PM by strangermike
May 11, 2017 03:24 PM|StrangerMike|LINK
Hello, below is my code to open an Excel file from a Gridview. As you can see it opens an .xls excel file on my Windows 7 PC.
However, some of our users have Windows Surface machines and we are finding they can't open the .xls files, only .xlsx files.
I changed the references to .xls to .xlsx but received this error. Can you help? Thanks!
Excel cannot open file TeamPerformance.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
' turn off paging
GridView1.AllowPaging = False
GridView1.AllowSorting = False
GridView1.ShowFooter = False
Excel_export = True
Response.AddHeader("Content-Disposition", "Attachment; FileName = TeamPerformance.xls")
Response.Charset = ""
Response.ContentType = "Application/vnd.xls"
' Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter()
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
' turn the paging on again
GridView1.AllowPaging = True
GridView1.AllowSorting = True
GridView1.ShowFooter = True
Excel_export = False
May 11, 2017 04:03 PM|PatriceSc|LINK
The problem is that you pretend that an HTML file is actually an XLS or XLSX file and Excel now warn about this kind of discrepancies.
Try perhaps to use .htm rather than .xls and keep the Excel mime type. Hopefully the browser will use the mime type to pass that to Excel that will see that all is ok and will import the HTML file. It will be less familier to users but should cause less
concern (at least they see this is not a real XLS or XLSX file).
If it doesn't work (or if it needs to really use the actual Excel file format) the only option I see would be then to create a "true" XLSX file for example using https://msdn.microsoft.com/EN-US/library/office/cc850837.aspx
May 11, 2017 05:08 PM|A2H|LINK
Alternatively If possible instead of using StringWriter logic to create excel file, try using a third party tool like
You can find the article written by Mike on
Exporting Gridview to excel
May 11, 2017 05:43 PM|StrangerMike|LINK
interesting. thank you