1

Generated xls file opens(in MS Excel 2013) with warning

The file format differs from the format that the file name extension specifies

, if press "Yes" - file perfectly opens, but I would like without this warning. The code fragment:

header("Content-Type: application/vnd.ms-excel; charset=windows-1251");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Disposition: attachment; filename=filename.xls);

echo<<<HTML
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1251" />
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0">
/* many lines */
</table>
</body>
</html>
HTML
die();
gutsout
  • 15
  • 5
  • See: http://stackoverflow.com/questions/30391879/file-format-and-extension-do-not-match/30393356#30393356 – Axel Richter May 27 '15 at 12:47
  • 1
    possible duplicate of [Excel spreadsheet generation results in "different file format than extension error" when opening in excel 2007](http://stackoverflow.com/questions/652377/excel-spreadsheet-generation-results-in-different-file-format-than-extension-er) – taxicala May 27 '15 at 12:49
  • 1
    That's because the generated xls file isn't a native format BIFF file, but simple html markup instead.... so MS Excel knows that you're telling it porkie pies trying to pretend that it's an xls file. The solution is either to tell the truth, and use an html extension for your file, or to create a real BIFF format xls file – Mark Baker May 27 '15 at 13:09

1 Answers1

2

As per Microsoft's support page:

The warning message is a user-notification function that was added to Excel 2007. The warning message can help prevent unexpected problems that might occur because of possible incompatibility between the actual content of the file and the file name extension.

So, having this on hand, your problem relies at that you are saving HTML contet with a file extension as .xls, thus, Excel will always throw that alert because the file content differs from the file extention, no matter what content type you specify.

https://support.microsoft.com/en-us/kb/948615

taxicala
  • 21,408
  • 7
  • 37
  • 66