本文介绍了PHPExcel无法读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用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()期望参数1为字符串,
数组给定[ROOT / vendor / PHPExcel / Classes / PHPExcel / IOFactory.php,line 225]
和
$ b b
file_exists()期望参数1是一个有效的路径,
数组给出[ROOT / vendor / PHPExcel / Classes / PHPExcel / Reader / Excel2007.php,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);
删除上述两个错误,但出现错误
无法打开testing.xlsx进行阅读!文件不存在。
这里, testing.xlsx
是我从表单中选择的文件
解决方案
添加文件如下:
require_once(ROOT。DS。'vendor'.DS.'PHPExcel / Classes / PHPExcel.php');
require_once(ROOT。DS。'vendor'。DS.'PHPExcel / Classes / PHPExcel / IOFactory.php');
y0您还需要添加一个命名空间如下
使用PHPExcel;
use IOFactory;
现在你可以访问excel类库并轻松地读写
i
感谢:)
I'm using CakePHP 3.2 and PHPExcel
library to import data from excel sheet to database.
I have the library at
/vendor/PHPExcel/Classes/PHPExcel.php
/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php
and the action in controller is
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);
}
}
But this gives error as
pathinfo() expects parameter 1 to be string,
array given [ROOT/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php, line 225]
and
file_exists() expects parameter 1 to be a valid path,
array given [ROOT/vendor/PHPExcel/Classes/PHPExcel/Reader/Excel2007.php, line 72]
and on debug($inputFileName);
it gives
[
'name' => 'testing.xlsx',
'type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'tmp_name' => '/tmp/phpvDWDxG',
'error' => (int) 0,
'size' => (int) 5247
]
replacing $inputFileName;
with $inputFileName['name']
in
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
removes above two error but gives error as
Could not open testing.xlsx for reading! File does not exist.
Here, testing.xlsx
is the file I'm selecting from form
解决方案
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
i am using this in my project.
you can try.
Thanks :)
这篇关于PHPExcel无法读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!