Yii excel export

It is very simple and a only a matter of a few lines of code to export your data (for example from mysql) into excel. Follow the following steps:
1. Download PHP Excel Library from
And extract into protected/extensions/PHPExcel

2. Use an action like the following in your controller

public function actionExcel($id) {
  $criteria = new CDbCriteria;
  $criteria->condition="winner_id=$id OR loser_id=$id";
  $criteria->order = "date DESC";
  $models= Match::model()->findAll($criteria);
  $objPHPExcel = new PHPExcel();

  // set header
  $objPHPExcel->getActiveSheet()->fromArray($data, NULL, 'A1');
  // from db
  foreach ($models as $model) {
     $data = [
      $objPHPExcel->getActiveSheet()->fromArray($data, NULL, 'A'.$i);
      $objPHPExcel->getActiveSheet()->setTitle('Matches list');
      foreach (range('A', $objPHPExcel->getActiveSheet()->getHighestDataColumn()) as $col) {


      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="matches.xls"');
      header('Cache-Control: max-age=0');
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

The phpexcel library can be dowloaded here PHPExcel.tar

Apache2 virtualhost 403 forbidden?

I was faced with this issue. But I didn’t like the idea of changing the group of my home directory to www-data. This problem can simply be solved by modifying the configuration file for the virtualHost. Simply configure the Directory tag to include these

   Order allow,deny
   Allow from all
   Require all granted

In my case I also needed to do this “chmod +x /home/(username)”