Click here to Skip to main content
14,486,704 members
Rate this:
Please Sign up or sign in to vote.
See more:
The file format and extension of filename.xls don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?.
I am using MVC. The data binding is happining at .CSHTML

how to avoid warning ?

What I have tried:

Response.AddHeader("content-disposition", "attachment; filename=DemoExcel.xls");  
Response.ContentType = "application/ms-excel";
Updated 4 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 1

There is nothing we can do to help: you need to look at the file, it's content, and if you can open it in Excel itself.

Chances are the file data you are sending or storing isn't a valid XLS file (it could be XLSX for example, or you could be storing it incorrectly.

Start by looking at the file content with a hex editor, opening it in Excel, and using the debugger to see exactly what you are transferring.

Sorry, but we can't do any of that for you!
Rate this:
Please Sign up or sign in to vote.

Solution 2

The problem is neither in the code you have provided nor in the usage of MVC; the problem looks to be in the content and how it is generated.

If you are just reading a file from a directory and sending it out as a stream; you will need to verify that the file is a valid XLS file with the XLS extension. If it is an XLSX file, you will need to alter your header tags respectively.

If you are programmatically creating this Excel document from data (aka Export), you need to validate that you are both creating a true Excel document AND that the type of Excel document matches the headers.
Here are a few ways this can be accomplished:
6 Easy Ways to Export Data to Excel in C# | Syncfusion Blogs[^]

Finally, if you are having problems creating a true Excel document; you may want to just fall back onto sending the data out as delineated (plain) text file- such as CSV. This does not require a program (such as Excel) to open it, but Excel is a default opener for it

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