传递给 PHPExcel_IOFactory::createWriter() 的参数 1 必须是 PHPExcel 的实例

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

我有一个问题。根据 https://github.com/segy/PhpExcel .

,我在 cakephp2.x 中使用 PHPExcel

现在我已经加载了 PHPExcel

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

我在控制器中这样做:

$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();

一切正常,excel 生成并保存到文件夹..但我收到了一些警告:

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]

现在,如果我是通过像这样的对象完成的:

    $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();

然后我得到这个错误

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

任何帮助将不胜感激。

你需要使用这条线

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

而不是

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

在控制器中。