toexcel Easily export to excel your data

  1. Requirements
  2. Usage
  3. Instalation
  4. Examples of use

Based on this wonderful extension: EExcelView

I decided to make a wrapper so everyone could easily export data to excel without needing to write some complex PHPExcel statements.

This extensions is a behavior you can attach to any controller so you can fetch some data from your models and export it to excel (and other formats) using the extension EExcelView.

As all the extensions (except PHPExcel) are included in this download you can consider this as a self-contained excel exporter. I tried to make sure that everything worked fine and tested with many configurations, but still issues could arise so I beg you comment so we can make the code better, both this extension and EExcelView

Requirements

Usage

In your controller/action

public function actionTest()
{
	// Load data
	$model = Classroom::model()->findAll();
	
	// Export it
	$this->toExcel($model);
}

This will generate an excel file with all the records of, in this case, Sala(Classroom) model with proper header and format. There are many options to allow many customizations so I will put some examples of use later

Instalation

  • Put PHPExcel so its folder looks like /protected/extensions/phpexcel/Classes/PHPExcel.php (in practice you can put it anywhere and then pass the pathAlias as a param to EExcelView, but for the sake of simplicity just place it there)

  • Put EExcelView so its folder looks like /protected/extensions/eexcelview/EExcelView.php

Note that I uploaded an improved version so you rather download that instead of the version posted in the extension page: Download here

  • Download EExcelBehavior and install it the same way as before, so it looks like /protected/extensions/eexcelview/EExcelBehavior.php

(Both extensions are included in the download package, except for PHPExcel, wich is huge)

Then, attach the behavior to the desired controller

public TestController extends CControler
{
	...

	public function behaviors()
	{
		return array(
			'eexcelview'=>array(
				'class'=>'ext.eexcelview.EExcelBehavior',
			),
		);
	}
	...
}

That's all :)

Examples of use

Filter data and export just the ID to Excel5 (.xls)

public function actionTest()
{
	// Load data (scoped)
	$model = Classroom::model()->notFull()->findAll();
	
        // Export it
	$this->toExcel($model,
		array(
			'id'
		),
		'Test File',
		array(
			'creator' => 'Zen',
		),
		'Excel5'
	);
}

Get some data from a CActiveDataProvider and export relations with correct headers titles to Excel2007 (xlsx)

public function actionTest()
{
	// Load data with a CActiveDataProvider (note that we can easily apply conditions over the result set)
	$model = new CActiveDataProvider('Classroom');
	
	// Export it (note the way we define columns, the same as in CGridView, thanks to EExcelView)
	$this->toExcel($model,
		array(
			'id',
			'name',
			'building.name::Building', // Note the custom header
		),
		'Test File',
		array(
			'creator' => 'Zen',
		),
		'Excel2007' // This is the default value, so you can omit it. You can export to CSV, PDF or HTML too
	);
}

Hope it helps you as much as it's helping me in my personal projects :)

19 0
49 followers
3 890 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Others
Developed by: zen∞
Created on: Mar 18, 2012
Last updated: 6 years ago

Downloads

show all