lujisheng
(Lujisheng)
March 8, 2014, 1:06am
1
I want to import/export data with PHPExcel, but i can’t load PHPExcel classes.
The classes of PHPExcel always have a prefix ‘phpexcel_’, Yii 2 seems don’t like it.
How to load PHPExcel into YII 2.0 project?
How can i use the PHPExcel library in a YII 2 project?
Could you give me a example?
thanks.
I have it with Composer:
Put this in the require bit:
"phpoffice/phpexcel": "dev-develop"
and do a composer update.
Then just use it in your code:
$objPHPExcel = new PHPExcel();
lujisheng
(Lujisheng)
March 8, 2014, 6:16am
3
thanks!
i have done as you said, but i have a error like this:
PHP Fatal Error – yii\base\ErrorException
Class ‘frontend\controllers\PHPExcel’ not found
kakalos12
(Kakalos12)
March 8, 2014, 5:06pm
4
use
$objPHPExcel = new \PHPExcel();
lujisheng
(Lujisheng)
March 9, 2014, 2:06am
5
Thank you!
It’s works!
Can you give me an example of import/export data with phpexcel in yii2 project?
lee140685
(Lee140685)
March 26, 2014, 8:15am
6
Thanks for sugestion, can you help giving example how to generate excel to browser using yii2. Thanks before
plantehal
(Plantehal)
March 26, 2014, 1:00pm
7
This is what it looks like in your view file. After it is install with composer, the extension will work exactly how it is described in the documentation. There is a lot of options for formatting your cells.
<?php
$objPHPExcel = new \PHPExcel();
$sheet=0;
$objPHPExcel->setActiveSheetIndex($sheet);
foreach ($foos as $foo) {
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->setTitle($foo->bar)
->setCellValue('A1', 'Firstname')
->setCellValue('B1', 'Lastname');
$row=2;
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$foo->firstname);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$foo->lastname);
$row++ ;
}
header('Content-Type: application/vnd.ms-excel');
$filename = "MyExcelReport_".date("d-m-Y-His").".xls";
header('Content-Disposition: attachment;filename='.$filename .' ');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
shark69
(Redsteva)
May 15, 2014, 8:21am
9
I’ve tried several ways, but the response is always PHPExcel_IOFactory 'not found. Are you sure it works?
shark69
(Redsteva)
May 15, 2014, 9:53am
10
Ok I found the problem,
I think we should improve the documentation of the installation and use of the class.
jotap28
(Jotap Tiago)
November 17, 2014, 12:39pm
11
Hi there!
I can use PHPExcel just like mentioned before
$objPHPExcel = new \PHPExcel;
However, I can’t get the content assist from eclipse working when I press ctrl+space.
For example when I’m tryng to type
$objPHPExcel->getActiveSheet()
if I press ctrl+space after “$objPHPExcel->” I don’t get any suggestions, probably meaning that content assist can’t see further into PHPExcel, as he sees other extensions;
Does anyone know how to sort this out?
tks in advance.
I have follow the same steps but every time it is giving error
PHP Fatal Error – yii\base\ErrorException
Class ‘PHPExcel’ not found
Following are my stpes:
[list=1]
[]I have added "phpoffice/phpexcel":" "
[*]in required section of www/project/yii-application/composer.json
[*]update composer
[*]in controller $objPHPExcel = new \PHPExcel(); or $objPHPExcel = new \PHPExcel; Both not working
[/list]
Please help
asmythe
(Asmythe)
August 27, 2015, 10:59pm
13
there are a few, very minor problems with your code. Here’s what I wrote and got to work:
$objPHPExcel = new \PHPExcel();
$sheet=0;
$objPHPExcel->setActiveSheetIndex($sheet);
$foos = [
['firstname'=>'John',
'lastname'=>'Doe'],
['firstname'=>'John',
'lastname'=>'Jones'],
['firstname'=>'Jane',
'lastname'=>'Doe'],
];
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->setTitle('xxx')
->setCellValue('A1', 'Firstname')
->setCellValue('B1', 'Lastname');
$row=2;
foreach ($foos as $foo) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$foo['firstname']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row,$foo['lastname']);
$row++ ;
}
header('Content-Type: application/vnd.ms-excel');
$filename = "MyExcelReport_".date("d-m-Y-His").".xls";
header('Content-Disposition: attachment;filename='.$filename .' ');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
Thanks very much for your example. It’s what I needed to get something basic working.
rayat2010
(Morshed Usa)
January 18, 2017, 10:00pm
14
Thank you, it worked with Oracle and Sql server as well. Here is working code:
public function actionExcel(){
Yii::import(‘ext.phpexcel.XPHPExcel’);
$objPHPExcel= XPHPExcel::createPHPExcel();
$objPHPExcel->getProperties()->setCreator("Morshedul Basher")
->setLastModifiedBy("Morshedul Basher")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$model = Yii::app()->db->createCommand("select AREA,REGION,Market,SWITCH from SWITCH_LOCATION")->queryAll();
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->setTitle('PSAP')
->setCellValue('A1', 'Area')
->setCellValue('B1', 'Region')
->setCellValue('C1', 'Market')
->setCellValue('D1', 'Switch');
$row=2;
foreach ($model as $foo) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$foo['AREA']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row,$foo['REGION']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$row,$foo['Market']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$row,$foo['SWITCH']);
$row++ ;
}
header('Content-Type: application/vnd.ms-excel');
$filename = "psap".date("d-m-Y-His").".xls";
header('Content-Disposition: attachment;filename='.$filename .' ');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
DarkSlash
(Darkslash)
August 2, 2017, 11:42pm
15
I can make it work but I want to get the file to download instead of writing on disk. What can I do?