excel-sheet-view a grid view exported in a spreadsheet

  1. Usage
  2. Sample Code

My wish is you never have to work with spreadsheets in your life! But if you do then this extension is for you. Inspired by CGridView, ExcelSheetView creates an excel spreadsheet from a data provider by using the power of PHPExcel library. ExcelSheetView has CBaseListView as its parent class. This widget comes along with a component similar to CDataColumn and it is called ExcelColumn

Download the zip file from the right sidebar. This file must be extracted inside "protected". My convention is that you have a "widgets" subfolder inside "protected" folder. This is why you see the alias 'application.widgets......' a lot inside the source code. If you want to use another location then you should change all these aliases inside the source code.

Usage

MAKE SURE you apply this hack: click here to your PHPExcel downloaded library before you use this extension

Afterwards you only have to import the php excel library only once as you see at the top of the ExcelSheetView class php file like this:

Yii::import('ext.phpexcel.PHPExcel',true);

If you are not familiar with the PHPExcel library then this is the home page of the PHPExcel project: PHPExcel

Also this wiki is a good place to start: How to use PHPExcel external library with Yii

Note: The subdirectory where the excel file will be written is the alias application.files Filename is depended on the title you set the widget attribute. The extension is xls.

Sample Code

So below we have an example of the widget. Assume we are inside a controller action. First of all we need a data provider. Let's choose a simple ArrayDataProvider

$rawData = array(
			array(
				'idCol' => 1,
				'columnName' => 'George',
				'columnSurname' => 'Pligor',
			),
			array(
				'idCol' => 2,
				'columnName' => 'No',
				'columnSurname' => 'Name',
			),
		);
$dataProvider = new CArrayDataProvider($rawData, array(
		    'id' => $this->action->id,
		    'keyField' => 'idCol',
		));

We will add some column configuration as well to display the power of ExcelColumn

$columns = array(
			array(
				'name' => 'columnName',
				'header' => 'Column Name',
				'footer' => 'george',
			),
			array(
				'name' => 'columnSurname',
				'header' => 'Column Sur Name',
			),
		);

Now assume we are inside the view:

$properties = array(
	'id' => $this->action->id .'-grid',
	'dataProvider' => $dataProvider,
	'columns' => isset($columns) ? $columns : array(),
	'showTableOnEmpty' => false,
	'title' => 'sample',
);
$this->widget('application.widgets.Excel.ExcelSheetView', $properties);

So finally you should be able to see something similar to this: > Total 2 results. > Please click here to download the file: sample.xls

And by clicking sample.xls a new excel file will be downloaded from the server! Well done!

Feel free to share any comments, remarks or notes you may have on this extension!

5 2
14 followers
2 050 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: User Interface
Developed by: pligor
Created on: Dec 12, 2011
Last updated: 12 years ago

Downloads

show all

Related Extensions