Export to file excel in cakephp


Posted on 2014-12-01 06:22:46


This tutorial about export file excel in cakephp. It is my experiences when I have used cakephp for two years. 1. The first you download file PhpExcelHelper.php at https://www.dropbox.com/s/rhebvuzl2roe9re/PhpExcelHelper.php?dl=0 You copy file PhpExcelHelper.php to folder app/View/Helper 2. The second you can download file Vendor_phpexcelhelper.zip then unzip  and copy folder PHPExcel & file PHPExcel.php in folder Export to file excel in cakephp  to app/Vendor https://www.dropbox.com/s/825ygo13pdzul3a/Vendor_phpexcelhelper.zip?dl=0 3. Then you work complete step 1 and 2. You can add
var $helpers = array('PhpExcel');
to file controller which is file controller, you want to work with that. 4. In file controller, you create a function for export to file excel. Example, I create a function admin_export in FarmerController.php 5. In file view, you create file view with name admin_export.ctp. Example for view of admin_export.ctp
<?php $this->PhpExcel->createWorksheet(); $this->PhpExcel->setDefaultFont('Calibri', 12); // define table cellsư $arrayContract = array( 0=>'No', 1=>'Yes' ); $table = array( array('label' => __('Farmer code'), 'width' => 'auto'), array('label' => __('Farmer name'), 'width' => 'auto', 'filter' => true), array('label' => __('Contract'), 'width' => 'auto', 'filter' => true), array('label' => __('Sourcing area'), 'width' => 'auto', 'filter' => true), array('label' => __('Village/city'), 'width' => 50, 'wrap' => true), array('label' => __('Supplier name'), 'width' => 'auto', 'filter' => true), array('label' => __('Years as farmer'), 'width' => 'auto', 'filter' => true), array('label' => __('History of farmer'), 'width' => 'auto', 'filter' => true), array('label' => __('Training received'), 'width' => 'auto', 'filter' => true), array('label' => __('Total area in m2'), 'width' => 'auto', 'filter' => true), array('label' => __('Status'), 'width' => 'auto', 'filter' => true), array('label' => __('Registration date'), 'width' => 'auto', 'filter' => true), ); if($this->Html->show($curUser, array(GROUP_AGRONOMIST))) { $table = $table; } else { $table = array_merge($table, array(array('label' => __('Agronomist'), 'width' => 'auto', 'filter' => true),array('label' => __('Approved date'), 'width' => 'auto', 'filter' => true), array('label' => __('Stopped date'), 'width' => 'auto', 'filter' => true))); } $this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true)); foreach ($farmers as $farmer) { $totalProductArea = 0; if(isset($farmer['FarmerField'])) { foreach($farmer['FarmerField'] as $item) { $totalProductArea += $item['area']; } } $row = array( $farmer['Farmer']['code'], $farmer['Farmer']['name'], $arrayContract[$farmer['Farmer']['contract']], $farmer['Area']['name'], $farmer['Farmer']['village_city'], $farmer['Supplier']['name'], $farmer['Farmer']['year_as_farmer'], $farmer['Farmer']['history_of_farmer'], $farmer['Farmer']['training_received'], $totalProductArea, $farmer['Farmer']['statusResult'], $farmer['Farmer']['registration_date'] ); if($this->Html->show($curUser, array(GROUP_AGRONOMIST))) { $row = $row; } else { $row = array_merge($row, array(substr($farmer['Farmer']['agronomist_name'], 0, -1), $farmer['Farmer']['approved_date'], $farmer['Farmer']['stopped_date'])); } $this->PhpExcel->addTableRow($row); } $this->PhpExcel->addTableFooter(); $this->PhpExcel->output('Farmer_export.xlsx'); ?>
6. Result for export to excel of me. farmer_export Thank you. You can post your ask on comment, I will answer your comment.