问题
I m trying to import 2010 xlsx file to mysql db in CAKEPHP. PHPExcel plugin is used for excel operations.
I m facing below issues.
Issue 1:: I see below error when my view is launched
Notice (8): Undefined index: Program
[APP\Controller\ProgramsController.php, line 83]
Issue 2:: On browsing xlsx file from view and when submit button is clicked i see below error. Could not open import.xlsx for reading! File does not exist.
Error: An Internal Error Has Occurred. Stack Trace
APP\Vendor\PHPExcel\IOFactory.php line 268 → PHPExcel_Reader_Excel2007->canRead(string)
APP\Vendor\PHPExcel\IOFactory.php line 205 → PHPExcel_IOFactory::createReaderForFile(string)
APP\Controller\ProgramsController.php line 87 → PHPExcel_IOFactory::identify(string)
[internal function] → ProgramsController->importexcel()
CORE\Cake\Controller\Controller.php line 490 → ReflectionMethod->invokeArgs(ProgramsController, array)
CORE\Cake\Routing\Dispatcher.php line 191 → Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 165 → Dispatcher->_invoke(ProgramsController, CakeRequest)
APP\webroot\index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Below is my code from controller
<?php
App::import('Helper', 'Number');
include '../vendor/PHPExcel.php';
include '../vendor/PHPExcel/IOFactory.php';
class ProgramsController extends AppController {
var $name = 'Programs';
var $helpers = array('Html', 'Form', 'Time','PhpExcel','Js' => array('Jquery') );
var $uses = array('Program','Customer');
public $components = array('PhpExcel');
function importexcel()
{
$inputFileName = $this->data['Program']['uploadFile'];
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'];
}
if ($this->Program->saveAll($resultarray)) {
$this->Session->setFlash(__('The Program have been saved', true));
}
}
}
}
?>
And my view code is as below.
<?php
echo $this->Form->create('Program',array('id' =>'importexcel','type'=>'post','action'=>'importexcel','url' => array('controller' => 'programs')));
?>
<table>
<tbody>
<tr>
<td>Choose Your File:</td>
<td><input width=200 type="file" name="data[Program][uploadFile]"></td>
<td>
<div>
<?php echo $this->Form->submit('Import Program', array('div'=>false,'name'=>'importexcel'));
?>
</div>
</td>
</tr>
</tbody>
</table>
Could anyone please help fix the issue, I searched online tutorials for uploading excel to mysql database using PHPExcel for CakePHP examples but could not find anything meaningful either.
If you could explain the steps for fix or any other method of doing it, it would be really helpful.
Thank you.
回答1:
The error you see is because the file path is incorrect. It comes from here: https://github.com/PHPOffice/PHPExcel/blob/develop/Classes/PHPExcel/Reader/Excel2007.php#82
So make sure you pass the right path to identify() function. What's in the $inputFileName variable? To be certain that the error comes from this, you can add a if (!file_exists($inputFileName)) { echo "NOT_THERE"; }
statement (or use breakpoints if you can.
Also, I'm not sure how CakePHP works, but when uploading a file, you need to use the path of the temporary file created. Try this:
$this->data['Program']['uploadFile']['tmp_name']
来源:https://stackoverflow.com/questions/28406298/phpexcel-plugin-does-not-import-file-in-cakephp