Skip to main content

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 plugin and read the tutorial.
But not get proper solution specially with relational data from one or many models (database tables). I try eexcelview, toexcel etc. yii plugin but it was really slow specially on large data fetch and also not getting proper excel format. After spending some time, i planed 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;
 $criteria->compare('vendor_debit_id',$this->vendor_debit_id);
 $criteria->compare('hotel_id',$this->hotel_id);
 $criteria->compare('supplier_name',$this->supplier_name,true);
 $criteria->compare('debit_amount',$this->debit_amount);
 $criteria->compare('bal_before_debit',$this->bal_before_debit);
 $criteria->compare('bal_after_debit',$this->bal_after_debit);
 $criteria->compare('debit_sheet_no',$this->debit_sheet_no,true);
 $criteria->compare('created_on',$this->created_on,true);
 $criteria->compare('admin_id',$this->admin_id);
 $data = new CActiveDataProvider($this, array(
 'criteria'=>$criteria,
 ));
 $_SESSION['vendor_debit_export_excel'] = new CActiveDataProvider($this, array('criteria' => $criteria, 'pagination' => false));;
 return $data;
}


2. Open controller class and define function for export in excel. Remember for unset session variable into the end for you controller function before end the function execution.
public function actionExport_excel()
{
 if(isset($_SESSION['vendor_debit_export_excel'])):
 header("Content-type: application/vnd.ms-excel");
 header("Content-Disposition: attachment;Filename=vendor-debit-report.xls");
 $dataProvider = $_SESSION['vendor_debit_export_excel'];
 $data = array();
 foreach($dataProvider->getData() as $record) {
 $data[] = array(
 'vendor_debit_id'=>$record->vendor_debit_id,
 'hotel_id'=>$record->hotel->name,
 'supplier_name'=>$record->supplier_name,
 'debit_amount'=>$record->debit_amount,
 'bal_before_debit'=>$record->bal_before_debit,
 'bal_after_debit'=>$record->bal_after_debit,
 'debit_sheet_no'=>$record->debit_sheet_no,
 'admin_id'=>Users::model()->usernameWithName($record->admin_id),
 '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));
 unset($_SESSION['vendor_debit_export_excel']);    
 Yii::app()->end();
 else:
 throw new CHttpException(500,'An error occurred in fetching data. please try later'); // Error message as per your convenient
 endif;
}


3. Now the final create and rederpartial view file into your relative view folder and pass the data(in array).
<html>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<body>
 <table border="1" cellpadding="5" style="font-size:10px;font-family:tahoma,arial;" cellspacing="0" bordercolor="#666666">
 <tr>
 <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>
 </tr>
 <?php
 if(isset($excel_data)):
 for($i=0;$i<count($excel_data);$i++):
 print_r($data);
 ?>
 <tr>
 <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>
 </tr>
 <?php
 endfor;
 endif;
 ?>
 </table>
</body>
</html>    


If you find any difficulty to problem in integration. Please put comment or mail us on info@ezeelive.com. We will feel happy to help you any problem relation with yii framework.

Comments

  1. I’m not sure where you’re getting your information and facts, but excellent subject. I need to make investments a while studying more or knowing more. Thanks for excellent information

    Wordpress Developers in Bangalore | Wordpress Development Bangalore

    ReplyDelete

Post a Comment

Popular posts from this blog

Overcoming Challenges in Adopting Generative AI in India

Adopting Generative AI in India presents several challenges, but it also offers significant opportunities for growth across various sectors. Here are some key obstacles and strategies to overcome them: 1. Infrastructure and Resource Limitations Challenge : India still faces issues with the availability of high-performance computing resources needed for generative AI models. This includes access to powerful GPUs and cloud services, which can be expensive. Solution : Partnerships with global tech firms can help bring advanced infrastructure to India. Government initiatives like the National AI Strategy and collaboration with research institutions can also boost local capabilities. 2. Data Privacy and Security Concerns Challenge : Data security is a significant concern, especially with the collection and usage of sensitive information. India's data protection laws, such as the Personal Data Protection Bill, are still evolving. Solution : Clear regulatory frameworks around data usage ...

Check and Use GZIP Compression through PHP

gzip compression is using for increase website speed and save server bandwidth. Read the below points to add gzip compression in your website: 1. Ask Hosting server provider to enable  gzip compresssion . 2. Compress all the css and js file using  7-zip  e.g. if your css file name is style.css it will save as style.css.gz. 3. Upload all the file on live server and set Content Encode GZIP to .gz files 4. Define  gzip global variable  in your common php file (remember the common file will include in all the php file into your website) $gzip_string=""; if (substr_count($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")) { $gzip_string=".gz"; }else{ $gzip_string=""; } 5. Put defined variable in all your script and style including in your php pages e.g. <link rel="stylesheet" type="text/css" href="css/my-style-file.css<?php echo $gzip_string;?>" /> <script type="text/javascript" ...

Why Indian PHP Development Companies Deliver Cost-Effective Solutions

In the world of web development, PHP remains one of the most popular programming languages, powering a vast majority of websites and applications. Businesses across the globe seek PHP Development Services to build scalable, secure, and high-performing web solutions. Indian PHP development companies have gained global recognition for delivering cost-effective solutions without compromising on quality. But what makes India a preferred destination for PHP development? Let’s explore the factors that contribute to the cost-effectiveness of Indian PHP development companies. 1. Competitive Labor Costs One of the primary reasons Indian PHP development companies can offer cost-effective solutions is the competitive labor cost. India has a vast talent pool of skilled PHP developers who are available at a fraction of the cost compared to developers in Western countries such as the United States, the United Kingdom, and Australia. This cost advantage allows businesses to allocate budgets more eff...