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.
Yes it is working .Fine Logic
Thanks blog.ekini.net Providers
Keep helping others
Unable to do this within WP. Any ideas?
@Dave E, WP == WordPress? Are you getting any error messages? We will need more details
Hi,
This is really helpful post. Thank you very much.
-Monika
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;
}
Pingback: CakePHP 2.x: CSV File download from a database query | Ekini.net by Wenbert Del Rosario