A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Zend Framework: View File downloaded as Excel File

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.

< ?php
header("Pragma: public");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: pre-check=0, post-check=0, max-age=0");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Transfer-Encoding: none");
header("Content-Type: application/vnd.ms-excel;");
header("Content-type: application/x-msexcel");
header("Content-Disposition: attachment; filename=report2_opendebitsummary".date('Ymd').".xls");
?>
 
< html>
< body>
< table border="1">
< tr>
    < th>Employee ID< / th>
    < th>Employee Name< / th>
< /tr>
< ?php
foreach ($data AS $row) :
?>
< tr>
    < td>< ?=$row['employee_id']?>< / td>
    < td>< ?=$row['employee_name']?>< / td>
< / tr>
< ?php
endforeach;
?>
< / table>
< / body>
< / html>

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

This entry was posted in General and tagged , . Bookmark the permalink.

6 Responses to Zend Framework: View File downloaded as Excel File

  1. Ratheesh says:

    Yes it is working .Fine Logic

    Thanks blog.ekini.net Providers

    Keep helping others

  2. Dave E says:

    Unable to do this within WP. Any ideas?

  3. Wenbert says:

    @Dave E, WP == WordPress? Are you getting any error messages? We will need more details :)

  4. Monika says:

    Hi,

    This is really helpful post. Thank you very much.

    -Monika

  5. ssegga says:

    I have setheader from the controller using the response object.

    function actionAction(){

    //disable layout
    $this->_helper->layout->disableLayout();

    //set the headers
    $this->_response->setheader(‘Pragma’, ‘public’)
    ->setheader(‘Cache-Control’,'no-store, no-cache, must-revalidate’
    )
    ->setheader(‘Content-type’,'application/x-msexcel
    ;

    //pass the output to the view
    $this->view->output = $dataForValue;

    }

  6. Pingback: CakePHP 2.x: CSV File download from a database query | Ekini.net by Wenbert Del Rosario

Leave a Reply to ssegga Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>