Zend Framework: View File downloaded as Excel File

Posted on: Apr 02, 2008 by wenbert

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.


Subscribe to comments Comment | Trackback |
Post Tags: ,

Browse Timeline


Comments ( 5 )

Yes it is working .Fine Logic

Thanks blog.ekini.net Providers

Keep helping others

Ratheesh added these pithy words on Oct 10 08 at 3:30 PM

Unable to do this within WP. Any ideas?

Dave E added these pithy words on Mar 27 09 at 9:25 AM

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

Wenbert added these pithy words on Mar 27 09 at 9:56 AM

Hi,

This is really helpful post. Thank you very much.

-Monika

Monika added these pithy words on Sep 12 09 at 4:26 PM

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;

}

ssegga added these pithy words on Oct 07 09 at 3:43 PM

Add a Comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">


© Copyright 2007 eKini Web Developer Blog . Thanks for visiting!