Custom export into excel in Yii Framework

Back to Blog
yii framework developer india - ezeelive

Custom export into excel in Yii Framework

Hello Friend, In one my project developed in Yii. I was struggling to export the cgridview data into excel. I searched many plugins and read the tutorial. But not get proper solution especially with relational data from one or many models (database tables). I try excelview, toexcel etc. Yii plugin but it was really slow especially on large data fetch and also not getting proper excel format. After spending some time, I planned to write my own custom export in excel code in Yii framework.

1. In model search function define session global variable and store active search data (set pagination false so you will get all the record without paging)

public function search()
 $criteria=new CDbCriteria;
 $data = new CActiveDataProvider($this, array(
 $_SESSION['vendor_debit_export_excel'] = new CActiveDataProvider($this, array('criteria' => $criteria, 'pagination' => false));;
 return $data;

2. Open controller class and define the function for export in excel. Remember for unset session variable into the end for you controller function before the end the function execution.

public function actionExport_excel()
 header("Content-type: application/");
 header("Content-Disposition: attachment;Filename=vendor-debit-report.xls");
 $dataProvider = $_SESSION['vendor_debit_export_excel'];
 $data = array();
 foreach($dataProvider->getData() as $record) {
 $data[] = array(
 'created_on'=>Yii::app()->dateFormatter->format("d/MM/y, h:m a", strtotime($record->created_on))
 echo $this->renderPartial('//hotelvendordebit/vendor_debit_excel',array('excel_data'=>$data));
 throw new CHttpException(500,'An error occurred in fetching data. please try later'); // Error message as per your convenient

3. Now the final create and rederpartial view file into your relative view folder and pass the data(in the array).

<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
 <table border="1" cellpadding="5" style="font-size:10px;font-family:tahoma,arial;" cellspacing="0" bordercolor="#666666">
 <td align="center"><strong>Debit ID</strong></td>
 <td align="center"><strong>Hotel</strong></td>
 <td align="center"><strong>Supplier Name</strong></td>
 <td align="center"><strong>Debit Amount</strong></td>
 <td align="center"><strong>Balance Before Debit</strong></td>
 <td align="center"><strong>Balance After Debit</strong></td>
 <td align="center"><strong>Debit Sheet No.</strong></td>
 <td align="center"><strong>Create/ Update User</strong></td>
 <td align="center"><strong>Created On</strong></td>
 <td><?php echo $excel_data[$i]["vendor_debit_id"];?></td>
 <td><?php echo $excel_data[$i]["hotel_id"];?></td>
 <td><?php echo $excel_data[$i]["supplier_name"];?></td>
 <td><?php echo $excel_data[$i]["debit_amount"];?></td>
 <td><?php echo $excel_data[$i]["bal_before_debit"];?></td>
 <td><?php echo $excel_data[$i]["bal_after_debit"];?></td>
 <td><?php echo $excel_data[$i]["debit_sheet_no"];?></td>
 <td><?php echo $excel_data[$i]["admin_id"];?></td>
 <td><?php echo $excel_data[$i]["created_on"];?></td>

If you find any difficulty to the problem in integration. Please put the comment or contact us. We will feel happy to help you any problem related with Yii framework.

Share this post

Comments (4)

  • Ecrva Reply

    I alաays spent my half an hour to read this webpage’ѕ content every day along with a cup of coffee.

    March 10, 2014 at 2:22 am
  • Kaceysaywel Reply

    Τhese are really impreѕsive ideas in on tҺe topic of bloggіng. You Һave toucҺed some fastidious points here. Any way keep up writing.

    March 11, 2014 at 8:59 pm
  • Anna Mathew Reply

    How to export a datatable in yii2 using phpexcel function?

    January 20, 2019 at 1:30 pm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Blog