Click here to Skip to main content
14,584,355 members
Rate this:
Please Sign up or sign in to vote.
Hello Friends,

I am trying to download an excel file template from my root Directory and the allowing user to edit the same and then upload the data entered into the modified excel sheet. But whenever i am downloading the template excel file, on opening it it give the following error: "Excel cannot open the file 'XYZ.xlsx' bacause the file format or file format or the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.".

Although the file extension (.xlsx) is fine and the file is obviously not corrupted as it is opening manually. Also, I would like to mention here that the same code is running just fine on another code file.

Kindly help me to resolve this issue.


What I have tried:

Dim strFilePath As String = Server.MapPath("~/Templates/XYZ.xlsx")
        Response.ContentType = "Application/xlsx"
        Response.AppendHeader("Content-Disposition", "attachment; filename=BankDetailTDS.xlsx")
Updated 19-Apr-20 2:39am
Richard Deeming 21-Apr-20 10:19am
NB: That's not the correct content type. You should be using:
Common MIME types - HTTP | MDN[^]
Varun Sareen 22-Apr-20 12:37pm
Ok Richard noted.

Rate this:
Please Sign up or sign in to vote.

Solution 1

We can't help you here: the most likely thing is that the file is corrupted, or you are trying to open it before it's fully downloaded, or ... There are a huge number of possible reasons.

Start by doing a binary file compare between the original template file and the downloaded file: if they are different then it's a transmission problem of some form. If they are the same, it's an excel problem of some form.

But we can;t help you with this one: we don't have any access to your systems or their files, so we really have no idea.
Varun Sareen 19-Apr-20 8:13am
ok thanks. But i assured that the file is actually not corrupted and the same code is running on different page.
Rate this:
Please Sign up or sign in to vote.

Solution 2

I found the solution to it. The issue was due to another Response.Write() code was written on Page load due to which the issue in excel file was coming.

Response.Write("<script language='javascript'>" & Chr(10))
Response.Write("</script>" & Chr(10))

The moment I commented the same, it worked.
Patrice T 19-Apr-20 10:11am
If question is solved, accept the solution to close the question.
Use Accept answer to close the question.

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100