我正在开发CakePHP 3.2。
我想从excel文件批量导入数据并将其保存到数据库。为此,我正在使用PHPExcel库。
我已经下载了库并在vendor
目录中解压缩,因此PHPExcel.php
的文件路径是
/vendor/PHPExcel/Classes/PHPExcel.php
和IOFactory.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);
}
}
}
注意:我从未使用过这样的插件和批量上传,这就是我在StackOverflow
上关注此问题的代码的原因现在,问题是,当我选择文件并上传时,会出现错误
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);
}
}
答案 0 :(得分:1)
我认为它是命名空间的一个简单问题。只需在名称类之前放一个斜杠,就像它在全局命名空间中一样
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName);
答案 1 :(得分:0)
您可以使用composer加载PHPExcel库。它将自动包含项目中的所有类。
composer require phpoffice/phpexcel
干杯:)