PHPExcel 无法读取文件
PHPExcel unable to read file
我正在使用 CakePHP 3.2 和 PHPExcel
库将数据从 excel sheet 导入数据库。
我的图书馆在
/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['name']
替换$inputFileName;
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
删除以上两个错误,但给出错误
Could not open testing.xlsx for reading! File does not exist.
这里,testing.xlsx
是我从表格
中选择的文件
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
我正在我的项目中使用它。
你可以试试
谢谢:)
我正在使用 CakePHP 3.2 和 PHPExcel
库将数据从 excel sheet 导入数据库。
我的图书馆在
/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['name']
替换$inputFileName;
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
删除以上两个错误,但给出错误
Could not open testing.xlsx for reading! File does not exist.
这里,testing.xlsx
是我从表格
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
我正在我的项目中使用它。
你可以试试
谢谢:)