0

我正在使用 CakePHP 3.2 和PHPExcel库将数据从 excel 表导入数据库。

我有图书馆

/vendor/PHPExcel/Classes/PHPExcel.php
/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php

控制器中的动作是

public function bulkUpload()
{
   $inputFileName = $this->request->data('excel_data');
   //debug($inputFileName['name']);
   if ($inputFileName != '') {
     $inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
     $objReader = \PHPExcel_IOFactory::createReader($inputFileType);

     $objReader->setReadDataOnly(true);
     $objPHPExcel = $objReader->load($inputFileName);
     $objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
     $highestRow = $objWorksheet->getHighestRow();

     for ($row = 2; $row <= $highestRow; ++$row) {
        $this->data['Program']['cycle_month'] = $objWorksheet->getCellByColumnAndRow(1, $row)->getValue();
        $this->data['Program']['cycle_year'] = $objWorksheet->getCellByColumnAndRow(2, $row)->getValue();
        $this->data['Program']['media_partnum'] = $objWorksheet->getCellByColumnAndRow(3, $row)->getValue();

        $resultArray[$row-2] = $this->data['Program'];
     }

      debug($resultArray);
  }
}

但这给出了错误

pathinfo() expects parameter 1 to be string, 
array given [ROOT/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php, line 225]

file_exists() expects parameter 1 to be a valid path, 
array given [ROOT/vendor/PHPExcel/Classes/PHPExcel/Reader/Excel2007.php, line 72]

并在debug($inputFileName);它上面给出

[
    'name' => 'testing.xlsx',
    'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'tmp_name' => '/tmp/phpvDWDxG',
    'error' => (int) 0,
    'size' => (int) 5247
]

替换$inputFileName;$inputFileName['name']_

$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);

删除以上两个错误,但给出错误为

 Could not open testing.xlsx for reading! File does not exist. 

testing.xlsx是我从表单中选择的文件

4

1 回答 1

0
You need to add file like below:

require_once(ROOT. DS .'vendor'.DS.'PHPExcel/Classes/PHPExcel.php');
require_once(ROOT . DS . 'vendor' . DS.'PHPExcel/Classes/PHPExcel/IOFactory.php');

y0ou also need to add a namespace like below

use PHPExcel;
use IOFactory;

Now you can access excel class library and easily read and write

我在我的项目中使用它。

你可以试试。

谢谢 :)

于 2016-08-23T17:17:44.370 回答