1

I'm trying to Export data from sqlServer to MS Excel file using vb.net framework 3.5 , everthing is working correctly and a save or open dialog opens up, when you save the file its saved but when trying to access the file i get a "Excel cannot open the file 'generated.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" message. I've got MS Office 2007 installed.

P.S I take a template Excel Document and merging data into it to produce another document.

My code is as follows

    Response.Clear()
    Response.ContentType = "application/ms-excel"
    Response.AddHeader("Content-Disposition", "attachment;filename=generated.xlsx")
    ExportToExcel(dt)
    Response.Flush()
    Response.End()

and added the following to the web.config file

1
  • Yes it is a duplicate, the answer on that question helped me. thanks for the link :) Commented Sep 23, 2012 at 6:43

1 Answer 1

2

You might try renaming the file to .xls. If that works then you need to either change the extension from .xlsx or more likely the mime type to

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

see http://technet.microsoft.com/en-us/library/ee309278(v=office.12).aspx for more MIME types for Office 2007.

To clarify my earlier post, did you go to the file that you have saved and change the extention to .xls? If you save an excel file as office 2003, then change the extension to .xlsx you get the error message "excel cannot open the file because the file format or file extension is not valid"

Sign up to request clarification or add additional context in comments.

1 Comment

I tried many suggested ContetType but it did not make any difference. i tried "octet-stream" and ""ms-excel" i got the very same behaviour.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.