Skip to content

Instantly share code, notes, and snippets.

@EricYue2012
Created December 6, 2013 00:23

Revisions

  1. EricYue2012 created this gist Dec 6, 2013.
    39 changes: 39 additions & 0 deletions cakephp_output_excel_file
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    //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');