Search code examples
cakephp-1.3

How to generate multiple excel sheets in a single document using cakephp


I am new to Cakephp I need to generate a Excel Report with tabs(sheets) .How can I generate multiple sheets in a single Document using cakephp. My sample Reports Controller having following Function

class ReportsController extends AppController {
    Function emp()
    {

    $this->set('crumbName', 'EMP');      

        $this->Emp->recursive = 2;
        $data = $this->Emp->find('all');
        $this->set('rows',$data);
        $this->render('emp_xls','export_xls');
    }

    ...
}

In emp_xls.ctp file contains employee information which is

    STYLE type="text/css">
    .tableTd {
        border-width: 0.5pt;
        border: solid;
    }

    .tableTdContent {
        border-width: 0.5pt;
        border: solid;
    }

    #titles {
        font-weight: bolder;
    }
    </STYLE>
    <table>
        <tr>
            <td><b>Faculty</b></td>
        </tr>
        <tr>
            <td><b>Date:</b></td>
            <td><?php echo date("F j, Y, g:i a"); ?></td>
        </tr>
        <tr>
            <td></td>
        </tr>
            <?php 
       error_reporting(0);
    echo '<tr id = "titles">';
  echo '<td class="tableTdContent">employee_number</td>';
  echo '<td class="tableTdContent">first_name</td>';
  echo '<td class="tableTdContent">last_name</td>';         
  echo '</tr>';
    ?>  

    <?php 
    foreach($rows as $data)
        {
     echo '<tr>';
 echo '<td class="tableTdContent">'. $data['Emp']['employee_number'].'</td>';
 echo '<td class="tableTdContent">'.$data['Emp']['first_name'].'</td>';
echo '<td class="tableTdContent">'.$data['Emp']['last_name'].'</td>';
echo '</tr>';
            }
                    ?>
    </table>

export_xls.ctp

<?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.xls" );
header ("Content-Description: Generated Report" );
?>
<?php echo $content_for_layout ?> 

this information will populate in excel sheet (Single Sheet), So now How can I generate two sheets in the same document to export the data?


Solution

  • Well, first of all if you do want to export data to an xls format you maybe want to use some component based in PHPExcel, like this I am sure you could find how to create several sheets for your XLS document in the documentation.

    If you want to show a kind of resume of the content of the excel (I think that's what you are trying to do with those table and td tags) probably you want something similar to tab in HTML. What about Bootstrap Tabs?