2016-07-29 48 views
0

我遇到了問題。我在cakephp2.x中使用PHPExcel根據https://github.com/segy/PhpExcel傳遞給PHPExcel_IOFactory :: createWriter()的參數1必須是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

任何幫助將升值。

回答

1

您需要使用此行

$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); 
在控制器

相關問題