0

I got DATA generated by PHP into HTML table format and useing the following code I EXPORT to EXCEL

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="BASIC_Data.xls"');
header('Cache-Control: max-age=0');

echo "<html>";
echo "<body>";
echo "<table class='table table-striped'>";
echo "<tbody>";

echo "<tr><td>$DATA</td></tr>";

echo "</tbody>";
echo "</table>";
echo "</body>";
echo "</html>";

it was working fine for 6 months,

NOW, I went to check and I see EXCEL is generated but no DATA.

I also checked if DATA is generated and I saw no problem.

problem is here:

header('Content-Type: application/force-download');
header('Content-Disposition: attachment;filename="BASIC_Data.xls"');
header('Cache-Control: max-age=0');

I don't know what is going on. suddenly everything changed

please help

7
  • What changed? What's in $DATA? What's up with the occasional word in uppercase? Commented Aug 15, 2016 at 13:23
  • @Daan $DATA is ok, no problem with it, it is basically foreach loop. it was working fine for 6 months, today I see it is generated excel with no data Commented Aug 15, 2016 at 13:25
  • Can you make a var_dump of $DATA. Commented Aug 15, 2016 at 13:27
  • @Daan did that too, no issue with data. its excel header Commented Aug 15, 2016 at 13:28
  • If you open the BASIC_Data.xls in an editor like Notepad++ it contains valid HTML? Or in a browser? But Excel is not displaying the tabular data correctly? I'm afraid, you are not outputting valid Excel. You're just writing HTML and relying on Excel's ability to interpret the data correctly, which might break anytime with a newer Excel version or different user settings. Try a good Excel library like PHPExcel. Commented Aug 15, 2016 at 13:42

1 Answer 1

1

Simply put: You are not outputting xls(x) data, but HTML. Excel can interpret tabular data in HTML, or at least used to but the correct way would be to generate a real Excel file with a decent library like PHPExcel instead of fooling your users into thinking they're downloading an XLS file when it's just a renamed HTML file. I know, JIRA does this as well, but it's still a bad solution.

Sign up to request clarification or add additional context in comments.

5 Comments

What you said is true and I agree. However, I have made the 25 functions on this and if I use PHPExcel I will have to change everything. Is there any temp solution while I work on it.
You don't have to change everything, just the "view layer", the output - which is pretty simple in your example. Just a bunch of echo statements. So, what is happening, exactly? You get an "Excel" file, it opens in Excel fine but then it's empty? "EXCEL is generated but no DATA. [...] I also checked if DATA is generated" So you've var_dump()ed the $DATA variable and it contains the data in HTML format?
Exactly! you read my mind. Excel file is created but not data and when I open the Excel file in Notepad it show the entire HTML Table with Data.
And this $DATA variable also has </td></tr> and <tr><td> tags in them? I quickly played around a bit with this based on what you posted and it works for me in Excel 2016. Can you post a snippet (anonymized if necessary) of the contents of $DATA so we can analyze it further?
it works, now, The issue was I think is that Microsoft has update its security. All you have to do now is to the download file and right button click and go to properties and check the UNLOCK

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.