我尝试将PHPExcel lib包含到Yii中,将PHPExcel.php
放在PHPExcel
文件夹附近的扩展名根目录中,并将该代码添加到config/main.php
// application components
'components'=>array(
'excel'=>array(
'class'=>'application.extensions.PHPExcel',
),
修改/protected/extensions/PHPExcel/Autoloader.php
public static function Register() {
$functions = spl_autoload_functions();
foreach($functions as $function)
spl_autoload_unregister($function);
$functions=array_merge(array(array('PHPExcel_Autoloader', 'Load')), $functions);
foreach($functions as $function)
$x = spl_autoload_register($function);
return $x;
}// function Register()
然后,尝试创建PHPExcel对象$objPHPExcel = new PHPExcel();
但在include(PHPExcel.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory
Z:\home\yii.local\www\framework\YiiBase.php(418)
答案 0 :(得分:4)
尝试这些,它对我有用。
ROOT\protected\vendors\PHPExcel\ PHPExcel.php PHPExcel\... all files php
$app = Yii::createWebApplication($config); // adding PHPExcel autoloader Yii::import('application.vendors.*'); require_once "PHPExcel/PHPExcel.php"; require_once "PHPExcel/PHPExcel/Autoloader.php"; Yii::registerAutoloader(array('PHPExcel_Autoloader','Load'), true); $app->run();
public function actionExcel(){ $objPHPExcel = new PHPExcel(); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'Hello') ->setCellValue('B2', 'world!') ->setCellValue('C1', 'Hello') ->setCellValue('D2', 'world!'); $objPHPExcel->getActiveSheet()->setTitle('Simple'); $objPHPExcel->setActiveSheetIndex(0); ob_end_clean(); ob_start(); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="test.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); }
答案 1 :(得分:1)
看到你的另一个问题,我想你已经找到了答案。唉......你必须在配置中使用导入,而不是组件。像这样:
// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
'application.vendors.phpexcel.classes.*',
),
您还应该将phpexcel放在名为vendors的文件夹中,而不是扩展名。它是第三方软件,不延长Yii。
此处还有一个指南可用于禁用Yii自动加载,而不是禁用PHPExcel自动加载。 http://www.yiiframework.com/wiki/101/how-to-use-phpexcel-external-library-with-yii/
答案 2 :(得分:1)
试试这个:
spl_autoload_unregister(array('YiiBase','autoload'));
Yii::import('ext.phpexcel.Classes.PHPExcel', true);
$objPHPExcel = new PHPExcel();
$activeSheet = $objPHPExcel->getActiveSheet();
spl_autoload_register(array('YiiBase','autoload'));