首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Cakephp 3中加载外部类和成员函数

问在Cakephp 3中加载外部类和成员函数
EN

Stack Overflow用户
提问于 2016-08-23 07:00:48
回答 2查看 995关注 0票数 0

我正在研究CakePHP 3.2。

我想从excel文件中大量导入数据,并将它们保存到数据库中。为此,我使用PHPExcel库。

我已经下载了这个库并在vendor目录中解压缩,因此PHPExcel.php的文件路径是

代码语言:javascript
复制
/vendor/PHPExcel/Classes/PHPExcel.php

IOFactory.php的文件是

代码语言:javascript
复制
/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php

我把这个包括在我的控制器里

代码语言:javascript
复制
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;

include '../vendor/PHPExcel/Classes/PHPExcel.php';
include '../vendor/PHPExcel/Classes/PHPExcel/IOFactory.php';

/**
 * Products Controller
 *
 * @property \App\Model\Table\ProductsTable $Products
 */
class ProductsController extends AppController
{
    public function beforeFilter(Event $event)
    {
        parent::beforeFilter($event);

        if ($this->Auth->user()['status'] != 1) {
            $this->Auth->deny(['sell']);
        }
        $this->Auth->allow(['bulkUpload']);
    }

    public function bulkUpload()
    {
       $inputFileName = $this->request->data('excel_data');


       if ($inputFileName != '') {
         $inputFileType = PHPExcel_IOFactory::identify($inputFileName); // line 33
         $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);
        }
    }
}

注意:我从未使用过这样的插件和批量上传,这就是为什么我在https://stackoverflow.com/questions/28406298/phpexcel-plugin-does-not-import-file-in-cakephp上遵循这个问题中的代码。

现在的问题是,当我选择一个文件并上传时,它会导致错误

代码语言:javascript
复制
 Class 'App\Controller\PHPExcel_IOFactory' not found at line 33

我认为问题在于调用PHPExcel_IOFactory类。

PHPExcel_IOFactory类在IOFactory.php文件中

代码语言:javascript
复制
<?php

/**    PHPExcel root directory */
if (!defined('PHPEXCEL_ROOT')) {
    /**
     * @ignore
     */
    define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
    require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
}

class PHPExcel_IOFactory
{
    /**
     * Search locations
     *
     * @var    array
     * @access    private
     * @static
     */
    private static $searchLocations = array(
        array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
        array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
    );

    /**
     * Autoresolve classes
     *
     * @var    array
     * @access    private
     * @static
     */
    private static $autoResolveClasses = array(
        'Excel2007',
        'Excel5',
        'Excel2003XML',
        'OOCalc',
        'SYLK',
        'Gnumeric',
        'HTML',
        'CSV',
    );

    /**
     *    Private constructor for PHPExcel_IOFactory
     */
    private function __construct()
    {
    }
    public static function identify($pFilename)
    {
       $reader = self::createReaderForFile($pFilename);
       $className = get_class($reader);
       $classType = explode('_', $className);
       unset($reader);
       return array_pop($classType);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-23 07:18:10

我知道这是名称空间的一个简单问题。只需将斜杠放在name类之前,因为它位于全局命名空间中。

代码语言:javascript
复制
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName); 
票数 1
EN

Stack Overflow用户

发布于 2016-08-23 10:53:37

您可以使用composer加载PHPExcel库。它将自动包含项目中的所有类。

代码语言:javascript
复制
composer require phpoffice/phpexcel

干杯:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39094375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档