-2

Here is the issue I have a simple PHP file that exports a table to an excel file with the extension ".xls". The code that makes the export goes like this:

header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Content-type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=filename.xls" );
header("Content-Description: PHP/INTERBASE Generated Data" );
header("Expires: 0");
Rest of the PHP code that has HTML tags for the table

This PHP class is called from a buton that goes like this:

<input type="submit" name="button2" onclick = "this.form.action = 'ExcelExportFile.php'" value='Export to Excel'>

The browser prompts for the file to either be downloaded or saved. The issue starts when trying to open the downloaded file depending on which Excel version is installed in the user's computer:

  • Excel 2010, Excel 2013: All it does is open Excel without opening the file, the program does not give any prompt or any warning, it just hangs on a grey window.
  • Excel 2016 opens up the file not before saying the user that the file might be corrupted and asks the user if it's sure to open the file. Click Yes and the file opens correctly showing the table

I have tried the troubleshoot from Microsoft's web page but nothing works. The interesting part about here is that if you go File -> Open -> Recent Books and click on the downloaded file, now it opens up correctly without making any changes on the file whatsoever.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
LuisR
  • 1
  • 2
  • could be an incorrect mime type. See this Q&A http://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document – Funk Forty Niner Aug 06 '16 at 00:04
  • 1
    you also have missing code; you are using `readfile()` and `exit;`, correct? you are reading these comments, correct? do show some sign of life. You never bothered answering the person who posted a comment under your other question http://stackoverflow.com/q/36168705/ and feel I'm also going to be ignored. So, I can't help you or anyone else for that matter, unless someone drops in a magic answer and you just run away with it. – Funk Forty Niner Aug 06 '16 at 00:10
  • I tried with all the mime types in that Q&A but neither worked. – LuisR Aug 06 '16 at 00:13
  • No I don't have readfile() nor exit – LuisR Aug 06 '16 at 00:14
  • That might be an issue. Here, see these Q&A's http://stackoverflow.com/q/10198524/ --- http://stackoverflow.com/q/11487365/ and make sure the delimiters are not at fault neither. Different Excel files from different countries (versions) will handle these quite differently. Make sure that there isn't a cache issue neither. – Funk Forty Niner Aug 06 '16 at 00:19
  • 1
    @pnuts I saw (remember) your comment about that the other day and you seem to have deleted it. I for one couldnt say since I don't work in the same environment as they do to execute their code. I found your comment about it to be interesting though. – Funk Forty Niner Aug 07 '16 at 04:42
  • 1
    @pnuts It's hard to say where they're at and the question. I gave up on it soon as I saw there wasn't anymore activity with it. – Funk Forty Niner Aug 07 '16 at 04:45

3 Answers3

0

Maybe you should specify text/csv as the content type with a .csv extension depending on your code (Excel will open csv just fine, if it's installed).

You really left out the most important code (the stuff in Rest of the PHP code that has HTML tags for the table).

Excel files are not HTML or tables so if you're outputting HTML or CSV and trying to say the content type is Excel, this is wrong.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • The interesing part here is that this issue just started a few days ago with excel in my company's computers. Because before that everyone was doing just fine exporting to excel until this last two days that issues began. I can put out a short version of the rest of the PHP script showing the HTML code for the table and the PHP code for the MySQL query that feeds the table, but I guess I didn't think it would be important. – LuisR Aug 06 '16 at 14:09
  • It might help, what are you outputting, HTML, csv, an Excel doc from a db? – drew010 Aug 06 '16 at 19:02
0

Microsoft have issued a couple of updates recently that stop it opening excel files that contain HTML. KB 3115322, 3115262 security updates

-1

Yes it is about the Excel updates since all the computers have this update, so I will have to find a way to circle around this problem in order to open these files so the users can keep working.

thanks

LuisR
  • 1
  • 2