PHPExcel插件不会在cakephp中导入文件 [英] PHPExcel plugin does not import file in cakephp

查看:100
本文介绍了PHPExcel插件不会在cakephp中导入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将2010 xlsx文件导入CAKEPHP中的mysql db. PHPExcel插件用于Excel操作.

I m trying to import 2010 xlsx file to mysql db in CAKEPHP. PHPExcel plugin is used for excel operations.

我正面临以下问题.

问题1 ::启动视图时出现以下错误

Issue 1:: I see below error when my view is launched

Notice (8): Undefined index: Program
[APP\Controller\ProgramsController.php, line 83]

问题2 ::在从视图浏览xlsx文件并单击提交"按钮时,我看到以下错误. 无法打开import.xlsx进行阅读!文件不存在.

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));

            }

        }

    }
}
?>

我的查看代码如下.

<?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>

任何人都可以帮助解决该问题,我搜索了在线教程,使用PHPExcel for CakePHP示例将excel上传到mysql数据库,但是也找不到任何有意义的东西.

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.

谢谢.

推荐答案

您看到的错误是因为文件路径不正确.它来自这里: https://github. com/PHPOffice/PHPExcel/blob/develop/Classes/PHPExcel/Reader/Excel2007.php#82

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

因此,请确保您通过正确的路径来标识()函数. $ inputFileName变量中包含什么?要确定错误是由此引起的,可以添加if (!file_exists($inputFileName)) { echo "NOT_THERE"; }语句(或者,可以使用断点.

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.

此外,我不确定CakePHP的工作方式,但是在上传文件时,您需要使用创建的临时文件的路径.试试这个:

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']

这篇关于PHPExcel插件不会在cakephp中导入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆