我正在研究CakePHP 3.2。
我想从excel文件中大量导入数据,并将它们保存到数据库中。为此,我使用PHPExcel库。
我已经下载了这个库并在vendor目录中解压缩,因此PHPExcel.php的文件路径是
/vendor/PHPExcel/Classes/PHPExcel.phpIOFactory.php的文件是
/vendor/PHPExcel/Classes/PHPExcel/IOFactory.php我把这个包括在我的控制器里
<?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上遵循这个问题中的代码。
现在的问题是,当我选择一个文件并上传时,它会导致错误
Class 'App\Controller\PHPExcel_IOFactory' not found at line 33我认为问题在于调用PHPExcel_IOFactory类。
PHPExcel_IOFactory类在IOFactory.php文件中
<?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);
}
}发布于 2016-08-23 07:18:10
我知道这是名称空间的一个简单问题。只需将斜杠放在name类之前,因为它位于全局命名空间中。
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName); 发布于 2016-08-23 10:53:37
您可以使用composer加载PHPExcel库。它将自动包含项目中的所有类。
composer require phpoffice/phpexcel干杯:)
https://stackoverflow.com/questions/39094375
复制相似问题