This project has moved. For the latest updates, please go here.

Excel cannot open file because the file format or file extension is not valid.

Sep 10, 2013 at 2:53 PM
I'm creating an excel sheet and trying to open it without saving it but it doesn't seem to work. If I save the file first and then open the file everything works fine though. Any suggestions? Here's my code...
Private Sub ExporttoExcel(table As DataTable)
            'your datatable
            Dim wb As New XLWorkbook()
            table.TableName = "export"
            wb.Worksheets.Add(table)
            'wb.SaveAs("export.xlsx")
           

            ' Prepare the response
            Dim httpResponse As HttpResponse = Response
            httpResponse.Clear()
            httpResponse.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            httpResponse.AddHeader("content-disposition", "attachment;filename=""export.xlsx""")

            ' Flush the workbook to the Response.OutputStream
            Using memoryStream As New MemoryStream()
                wb.SaveAs(memoryStream)
                memoryStream.Position = 0
                memoryStream.CopyTo(httpResponse.OutputStream)
                'memoryStream.WriteTo(httpResponse.OutputStream)
                memoryStream.Close()
            End Using

            httpResponse.End()
        End Sub
Coordinator
Sep 10, 2013 at 4:00 PM
I don't understand, you're trying to "open" an Excel file without the file existing on disk? Excel can only open files on disk.
Sep 10, 2013 at 4:05 PM
If you're in IE you get that nice little toolbar to pop up at the bottom of the window that gives you the option to either "Open" the file or "Save" it. When I click "Open" I get that error message, if I first "Save" the file it opens just fine.
Coordinator
Sep 10, 2013 at 4:13 PM
1) Why not use WriteTo OutputStream?
2) What happens when you get the download dialog box and you click on "Open"? Does Excel opens and tries to open the file but can't?
Sep 10, 2013 at 4:19 PM
1) Same error message is generated.
2) Excel opens but the file does not and when I check my "Downloads" folder the file is not there.
Coordinator
Sep 10, 2013 at 4:29 PM
Sep 10, 2013 at 4:32 PM
First I'm prompted with a dialog in the center of the browser with the options to "Open", "Save", and "Save As". When I select open the toolbar opens at the bottom of the window showing progress, then excel opens and the spread opens in "Protected View". This is what I'm looking to accomplish. I wonder if it has anything to do with me running the code on localhost?
Coordinator
Sep 10, 2013 at 4:41 PM
I officially have no idea. The file is created correctly (since you can open it after saving it explicitly). But after you click "Open" instead, something's happening between IE saving the file in the temp folder and Excel opening it up. Have you tried it with Firefox or Chrome?
Sep 10, 2013 at 4:43 PM
Negative, because it has to work in IE minimally. One question about your link though, you have the file in the link specified as an excel file. I'm creating mine when clicking a LinkButton on an ASPX page, could this have anything to do with it?
Coordinator
Sep 10, 2013 at 5:03 PM
Shoot, you're right. It's being served differently.

I don't know how to help you but I'm pretty sure it has something to do with your setup. That's because a lot of people serve Excel files in the same way without a problem.

Sorry I can't be of more help.
Sep 10, 2013 at 5:06 PM
No worries, I'll figure it out eventually. :)
Sep 10, 2013 at 8:04 PM
FYI, once I deployed my app to my production web server everything worked and fell into place so obviously something is up with my local setup. Thx for all your help earlier.
Coordinator
Sep 10, 2013 at 8:21 PM
I'm glad you're up and running :)
Oct 3, 2014 at 4:15 PM
mcconnelljh wrote:
FYI, once I deployed my app to my production web server everything worked and fell into place so obviously something is up with my local setup. Thx for all your help earlier.
Just so that it's on the record, I'm running into a similar problem except in reverse. It works on my local environment but not on the production environment. If you have any information as to why this happened, it would be appreciated.