Poonam
Poonam

Reputation: 549

Argument 1 passed to PHPExcel_IOFactory::createWriter() must be an instance of PHPExcel

I had a problem. I'm using PHPExcel in cakephp2.x According to https://github.com/segy/PhpExcel .

Now I had loaded PHPExcel

public $helpers = array('PhpExcel');
public $components = array('PhpExcel');

I'm doing this in controller:

$this->PhpExcel->createWorksheet()->setDefaultFont('Calibri', 12);
$this->PhpExcel->getActiveSheet()->setTitle('SSUK New booking');

// define table cells
$table = array(
           array('label' => __('Student First Name')),
           array('label' => __('Student Last Name')),
           array('label' => __('Student Group')),
       );
$this->PhpExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true));
$this->PhpExcel->addTableRow(array(
                        'abcd',
                        'qwerty',
                        'cts',
                      ));
$this->PhpExcel->addTableFooter();
$objWriter = PHPExcel_IOFactory::createWriter($this->PhpExcel, 'Excel5');
$excel_name = 'new_booking.xlsx';
$objWriter->save('img/excel/' . $excel_name);
exit();

everything is working perfect, excel generates and save to folder.. but I got some warnings:

Warning (4096): Argument 1 passed to PHPExcel_IOFactory::createWriter() must be an instance of PHPExcel, instance of PhpExcelComponent given, called in D:\xampp\htdocs\SSUK\app\Controller\ChildFormsController.php on line 2012 and defined [APP\Vendor\PHPExcel\IOFactory.php, line 132]

Warning (4096): Argument 1 passed to PHPExcel_Writer_Excel5::__construct() must be an instance of PHPExcel, instance of PhpExcelComponent given, called in D:\xampp\htdocs\SSUK\app\Vendor\PHPExcel\IOFactory.php on line 141 and defined [APP\Vendor\PHPExcel\Writer\Excel5.php, line 106]

Warning (4096): Argument 1 passed to PHPExcel_Calculation::getInstance() must be an instance of PHPExcel, instance of PhpExcelComponent given, called in D:\xampp\htdocs\SSUK\app\Vendor\PHPExcel\Writer\Excel5.php on line 123 and defined [APP\Vendor\PHPExcel\Calculation.php, line 1762]

Warning (4096): Argument 1 passed to PHPExcel_Calculation::getInstance() must be an instance of PHPExcel, instance of PhpExcelComponent given, called in D:\xampp\htdocs\SSUK\app\Vendor\PHPExcel\Writer\Excel5.php on line 124 and defined [APP\Vendor\PHPExcel\Calculation.php, line 1762]

Warning (4096): Argument 1 passed to PHPExcel_Writer_Excel5_Workbook::__construct() must be an instance of PHPExcel, instance of PhpExcelComponent given, called in D:\xampp\htdocs\SSUK\app\Vendor\PHPExcel\Writer\Excel5.php on line 134 and defined [APP\Vendor\PHPExcel\Writer\Excel5\Workbook.php, line 203]

Warning (4096): Argument 1 passed to PHPExcel_Calculation::getInstance() must be an instance of PHPExcel, instance of PhpExcelComponent given, called in D:\xampp\htdocs\SSUK\app\Vendor\PHPExcel\Writer\Excel5.php on line 229 and defined [APP\Vendor\PHPExcel\Calculation.php, line 1762]

Now if I had done it through object like:

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->createWorksheet()->setDefaultFont('Calibri', 12);
    $objPHPExcel->getActiveSheet()->setTitle('SSUK New booking');

    // define table cells
    $table = array(
              array('label' => __('Student First Name')),
              array('label' => __('Student Last Name')),
              array('label' => __('Student Group')),
             );
   $objPHPExcel->addTableHeader($table, array('name' => 'Cambria', 'bold' => true));
   $objPHPExcel->addTableRow(array(
                        'abcd',
                        'qwerty',
                        'cts',
                        ));
   $objPHPExcel->addTableFooter();
   $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
   $excel_name = 'new_booking.xlsx';
   $objWriter->save('img/excel/' . $excel_name);
   exit();

Then I got this error

Fatal Error

Error: Call to undefined method PHPExcel::createWorksheet() File: D:\xampp\htdocs\SSUK\app\Controller\ChildFormsController.php Line: 1917

Notice: If you want to customize this error message, create app\View\Errors\fatal_error.ctp

Any help will be appreciate.

Upvotes: 0

Views: 4490

Answers (1)

Manmohan
Manmohan

Reputation: 740

You need to use this line

$this->PhpExcel->getWriter('Excel5');
 $excel_name = 'new_booking.xlsx';
 $this->PhpExcel->save('img/excel/' . $excel_name);

instead of

$objWriter = PHPExcel_IOFactory::createWriter($this->PhpExcel, 'Excel5');
$excel_name = 'new_booking.xlsx';
$objWriter->save('img/excel/' . $excel_name);

in controller.

Upvotes: 1

Related Questions