Zend Framework: View File downloaded as Excel File

Filed Under (General) by Wenbert on 02-04-2008

Tagged Under : ,

This is basically a back-up post from my previous posts regarding exporting of data from a database to an excel file. Since Excel is able to read HTML tables, we can directly export a “view” file into an excel file by changing the headers of the request.

  1.  
  2. < ?php
  3. header("Pragma: public");
  4. header("Cache-Control: no-store, no-cache, must-revalidate");
  5. header("Cache-Control: pre-check=0, post-check=0, max-age=0");
  6. header("Pragma: no-cache");
  7. header("Expires: 0");
  8. header("Content-Transfer-Encoding: none");
  9. header("Content-Type: application/vnd.ms-excel;");
  10. header("Content-type: application/x-msexcel");
  11. header("Content-Disposition: attachment; filename=report2_opendebitsummary".date(‘Ymd’).".xls");
  12. ?>
  13.  
  14. < html>
  15. < body>
  16. < table border="1">
  17. < tr>
  18.     < th>Employee ID< / th>
  19.     < th>Employee Name< / th>
  20. < /tr>
  21. < ?php
  22. foreach ($data AS $row) :
  23. ?>
  24. < tr>
  25.     < td>< ?=$row[‘employee_id’]?>< / td>
  26.     < td>< ?=$row[‘employee_name’]?>< / td>
  27. < / tr>
  28. < ?php
  29. endforeach;
  30. ?>
  31. < / table>
  32. < / body>
  33. < / html>
  34.  

It is important that you disable layouts and other stuff that will directly change the output of the view file.

Leave a Reply