Skip to content

Instantly share code, notes, and snippets.

@EricYue2012
Created December 6, 2013 00:23
Show Gist options
  • Save EricYue2012/7816628 to your computer and use it in GitHub Desktop.
Save EricYue2012/7816628 to your computer and use it in GitHub Desktop.
How to generate excel file in cakephp
//excel layout
<?php
header ("Expires: Mon, 28 Oct 2008 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename=\"report_".date('dmy_His').".xls" );
header ("Content-Description: Generated Report" );
?>
<?php echo $content_for_layout ?>
//$content_for_layout example : basic table
<table>
<tr>
<th width="10%">Guest Name</th>
<th width="10%">Partner Name</th>
<th width="10%">Company Name</th>
</tr>
<?php
foreach ($bookings as $booking):
?>
<tr>
<td>
<?php
echo $booking['Guest']['name'] . ' ' . $booking['Guest']['surname'];
?>
</td>
<td>
<?php
echo $booking['Partner']['name'] . ' ' . $booking['Partner']['surname'];
?>
</td>
</tr>
<?php endforeach; ?>
</table>
//controller: the layout 'excel' is the one defined above
$this->render($render,'excel');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment