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 ¶
- Yii 1.1 or above
- PHPExcel
- EExcelView (included)
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 :)
great work , both this and the EExcelView is wonderful ,thanks for sharing .
besides the static behavior config , you can also dynamic attach this behavior to you controller :
// just in my XxxController::actionAdmin() : $bh = Yii::createComponent(array( 'class'=>'ext.eexcelview.EExcelBehavior', )); $this->attachBehavior('excelExport',$bh); $this->toExcel($model->search(), array( ), 'Test File', array( 'creator' => 'yiqing', ), 'Excel2007' // This is the default value, so you can omit it. You can export to CSV, PDF or HTML too ); //thus we can keep the modify in a method !
some one who is interested in interoperate with the admin view( created by gii/giix) , you can refer to here csvExporter
NOTE: in YII_DEBUG mode some debug info may be printed to the file , you need to do some minor modifying :
in EExcelView :
Yii::app()->end(); =====>> die();
Amazing
Thank you, everything I tried was working perfectly!
You have saved many hours of coding!
Nice Work!
This is a nice work! :)
No buffer to delete
Hei!
I was getting this pretty stupid error in file EExcelView.php on line 257:
~~~
ob_end_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete
~~~
It's much better (even written in manual) if you go through all handlers and close them if any:
while ( ! empty($handlers) ) { ob_end_clean(); $handlers = ob_list_handlers(); }
This works fine.
Cool
Moved from another extension to this and it was easier than I thought.
Works great for now :P (I've not broken it yet)
HTML encoding of characters
I have a CArrayDataProvider where some fields' values have "&" and '(single quotes)
The value in the excel file show html encoding of this characters:
"&"
"'"
Does anyone know how I can fix this ?
Thanks
$rawData = Yii::app()->db->createCommand($sql)->queryAll(); $dataProvider=new CArrayDataProvider($rawData
Allowed memory size problem
Hi! I tried to use your extension but PHPExcel library gives me the error that Allowed memory size exhausted. I have read some different things about this but I am not sure how to resolve it. I had a lot of data and has to be exported as CSV.
Another thing which is not clear for me(if the first one will be resolved) is that i need to export the filtered data from gridview. Do you think this is possible?
Thank you,
Giving fixed
@denis84
Actually that is an issue with php, you can configure the memory limit in php.ini file, or with the function ini_set('memory_limit', '100M'); Beware that you need to have the required permissions if you are in a shared server.
@yisus
I'll make sure to take a look into that, for now, you can try to put the type raw into the field you are printing (eg: columns( ... 'field:raw:Label', ... ) that should do the job)
Allowed memory size problem
I already tried that with a bigger value and nothings changed.
And what about the second question? about filters...
Thanks,
denis84
Sorry, I forgot to answer that.
You cand always do something like this:
$model = YourModel('search'); $model->type = 1; // This will filter out all the results whose type is 1 $dataProvider = $model->search(); // Then you just perform the search and obtain the data provider.
Onto the first question. It's clearly a memory issue that should be resolved increasing the memory available. I'm concerned about PHPExcel excesive memory usage, thou, so I think an "iteration over chunks" solution may be on the road to solve this. I'm not an PHPExcel expert, but I'll try to hack more into it and squeeze the better performance out of it.
Aside: if you need many information and you just need to export to csv I think you could easily write a SQL query to do it for you. Is this an acceptable workaroud for you?
Hope I helped a bit.
Formatting cell
Thanks for this awesome extension.
i need to know is there any way to format the cell value e.g i want to display a digit as 10000000000 instead of 1.e10 etc.
Thanks. Now about date
Thank you zenbaku
Using 'field:raw:label' did the trick.
By the way, do you know how to set a cell to be a date?
I have '3/3/2012' strings that I'm trying to set as date, but using 'field:date:label' will break.
Thanks again.
bug fix with get relations data
EExcelView.php
line 141:
to
$value=$this->evaluateExpression($column->value ,array('row'=>$row,'data'=>self::$data[$row]));
I FOUND THIS PROBLEM
I found this problew
Fatal error: Call to a member function getAttributeLabel() on a non-object in /WEBSITE/www/yii/library/admin/protected/extensions/eexcelview/EExcelView.php on line 96
how to fix it?
string format
i also want to display a digit as 10000000000 instead of 1.e10 and also stop the leading zeroes getting truncated as it is a UPC field, can someone help please.
cgridview search data
One of the previous comments asked about filtered data, however I still cannot get it to work with that solution.
In my controller I have
However, the csv being exported only contains the default records of the model from the first page of cgridview.
Any help would be greatly appreciated.
how to get excel at particular path
I do not get,where it save excel file,how I can export excel to particular path.
pagination
In my gridview ,only for examole,10 out of 100 records are there,I want to export that 10 values only,not all values by findall,can u tell me how I can achieve it.
pagination
I have done 10 out 100 recoeds,but now,I want next 10 records,means in gridview suppose I have ,20-30 id,10 number of records,It displays only first 10, do not know ,How to get.
Error
include(PHPExcel_Cell.php): failed to open stream: No such file or directory
I am getting this error please help me.
Great Extension
I am experiencing a slow response with 4 foreign key fields to tables for things like "status" or "category".
I have a problem when the user does not filter the data by at least one column, the controller seems to timeout and I am left with a blank page. Works fine if user selects all rows were status = 'Active' or any other column.
I only have 1500 rows in the table...so that data set is trivial in size. I am not a MySQL guy, but have 25 years of experience with designing Oracle applications. All my tables have primary keys and none of the tables used
for lookup are large (500 rows max, 10 rows average).
Any suggestions?
Thanks!
Blank page problem
@AustinGeek most likely the problem lies in the memory usage, as creating an excel sheet uses quite a lot of memory, even for a small number of rows, due to the fact that it keeps a lot of info, regarding formatting for each cell. Try increasing your memory_limit from php.
Issue in Datetime DataType field Issue in Excel
Hello Community,
I have an issue export data in excel. Whenever i trying to export datetime field record like : 2012-10-12 17:10:10, i am getting this field value like this :
12 17:10:10 /11/2012 .
I think there is an issue in change the format of excel column. Please help me to solve this issue.
Thanks in Advance .
dashes and slashes....
Great / Simple Extension:
One problem I found the other day.
I have a url in a column that I am exporting....it does the same thing as karmraj described with his date issue.
"the-web-times.com" is exported to excel (and csv) as "times.com\web\the"
I looked thru the code and didn't see anything that would trigger this type of transformation. I also contacted Bennounna and asked him about it.
Any ideas Zen?
Thanks !
Check EExcelView file of extension
@AustinGeek : Please check out EExcelView extension file. After line no 202, you can see following code.
//date edited francis $dateF= explode("-", $value); $c1=count($dateF); if($c1==3 && $dateF[0]<9000 && $dateF[1]<13 && $dateF[2]<32)//{} $value=$dateF[2].'/'.$dateF[1].'/'.$dateF[0]; //end of date
This code use for format the date field value which you need to export in excel. And in your issue this code interpret your URL value as a date field value that why it convert your URL value as "/" saperated value.
Refere this logic.
date reformatting
Thanks Karmraj !
Problem is definitely in EExcelview. I just don't understand why a varchar column with url in it is being interpreted as a date.
Need to study it a bit and find out how that column is being processed by that particular logic.
I am also having a big problem with a simple export to CSV or EXCEL format (2007 in particular) It uses 128M and blows up! The data was 1000 rows of 15-20 columns (no big text fields).
I don't know if there is a way to tune phpexcel
can't find the file!
i have a new bee question : where is the folder that should contain the exported file ?
can't find the file!
i have a new bee question : where is the folder that should contain the exported file ?
Great!
Thank you, that's great!
I got error "failed to delete buffer. No buffer to delete" but solve it by replacing "ob_end_clean()" with "if (ob_get_contents()) ob_end_clean()" in line 257 of EExcelView.php
masterfouss
Changing Value
As like overriding the header values, is there any way to change the value of data while passing it to the extension.
Ex: Changing 'Name' or modifying it a bit.
array(
'id', 'name', 'building.name::Building', // Note the custom header ),
Please let me know how to achieve this. THanks.
how to convert to pdf
how to convert to pdf??
when I change to PDF it make error said
"PDF Rendering library has not been defined."
Thanks For great extension, but i have a problem
Thank you for such a great extension, but when i download the file, it is blank.
I am not getting any data there.
How can i sort out this?
how to create multiple sheets?
is there anyway to create multiple sheets using this plug-in?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.