Why showing error message while opening .xls file

In my asp.net, C# application we are generating and downloading .xls file. But when I’m trying to open, it’s giving a message

“The file you are trying to open, ‘filename.xls’, is in a different
format than specified by the file extension. Verify that the file is
not corrupted and is from a trusted source before opening the file. Do
you want to open the file now?”

If I press ‘Yes’ it’s opening. I changed the file extension to .xlsx, still same message. Can anyone tell me why this is happening? I’ve added .xlsx MIME type extension in IIS manager with MIME Type as application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. Still it’s showing the same message. Please suggest how can I get rid off it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This one might be an old post, but I’ve been searching answers for the same issue, and thought it could be useful. It looks it’s not related to the way your application gives the xls file to the browser. It’s an issue with Office 2007 excess of security.

Microsoft’s KB article 948615 explains it:

When you open a file in Excel 2007, you receive a warning that the
file format differs from the format that the file name extension
specifies.

The only solution is to add en entry to the client’s registry. I know that’s not a real solution for a web application maybe with hundreds of users, but at least you can add a note to the page telling how to fix that annoying warning.

Method 2

Do you completely create the xls file or do you copy and fill a template xls file ?
An incorrect format template file may cause the problem.

Also, what provider do you use to fill your file ?
– Microsoft.ACE.OLEDB.12.0 for xlsx/xlsm ?
– Microsoft.Jet.OLEDB.4.0 for xls ?

An incorrect provider/extension combination may cause the problem.

According to your comment, here is a part of code where I have done that in the past :
( commented some of the lines as I don’t remember why they where useful )

Response.Clear();                           
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentEncoding = Encoding.Default; 
//Response.Charset=""; 
Response.AddHeader("Content-Disposition: ",
    String.Format(@"attachment; filename={0}",myfileName));

//EnableViewState = false; 

Response.Write(myFileContentAsString); 
Response.Flush();
Response.Close();

Method 3

The file extension .xls (Version 2003) supports the HTML layout whereas office 2007 and above versions do not support it.

You must export the excel file to .xlsx format. From my experience it will then support it in all versions.

Add below DLLs into bin folder

  • ClosedXML.dll
  • DocumentFormat.OpenXml.dll

Code to export file to .xlsx

    DataTable dt = new DataTable(); 
    //Create column and inser rows
    using (XLWorkbook wb = new XLWorkbook())
    {           
                var ws = wb.Worksheets.Add(dt, Sheetname); 
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Charset = "";
                HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + p_FileName + ".xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(HttpContext.Current.Response.OutputStream);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                }
     }

Method 4

Not sure how you are exporting data to excel. Setting the DisplayAlerts to false might help.

xlApp = new Excel.Application();
xlApp.DisplayAlerts = false;

I use interop DLL. The namespace would be Microsoft.Office.Interop.Excel.
See Feng Chen’s answer in the following link on how to add the reference: http://social.msdn.microsoft.com/Forums/en/netfxsetup/thread/c9e83756-4ae2-4ed4-b154-1537f3bb3a22

The following link might also help:

http://www.dotnetperls.com/excel

Method 5

The alert is a new security feature in Excel 2007 called Extension Hardening, which ensures that the file content being opened matches the extension type specified in the shell command that is attempting to open the file.
The current design does not allow you to open HTML content from a web site in Excel unless the extension of the URL is .HTM/.HTML/.MHT/.MHTML. So ASP pages that return HTML and set the MIME type to something like XLS to try to force the HTML to open in Excel instead of the web browser (as expected) will always get the security alert since the content does not match the MIME type.

http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/03/11/excel-2007-extension-warning.aspx

Method 6

You are using .xls in filename and .xlsx in the MIME type. Try with filname.xlsx.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x