文章来给大家介绍在yii框架中利用phpexcel插件来快速实例导出excel数据的具体方法,有在使用yii的同学不防进入参考一下。
最近在研究php的yii框架,很喜欢,碰到导出excel的问题,研究了一下,就有了下面的方法:
1、首先在cofig/main.php中添加对phpexcel的引用,我的方式是这样:
代码如下 复制代码
// autoloading model and component classes
'import'=>array(
/*'application.modules.srbac.controllers.sbasecontroller',*/
'application.models.*',
'application.components.*',
'application.extensions.phpexcel.*',
),
2、当然要记得将phpexcel整个目录复制到项目的 protected/extensions/ 目录下面。
3、按照下面的代码修改phpexcel代码目录里的autoloader.php文件:
代码如下 复制代码
public static function register() {
/*if (function_exists('__autoload')) {
// register any existing autoloader function with spl, so we don't get any clashes
spl_autoload_register('__autoload');
}
// register ourselves with spl
return spl_autoload_register(array('phpexcel_autoloader', 'load'));*/
$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()
上面的函数中,注释掉的是原有的代码。
4、下面的代码是输出excel,以及一些常用的属性设置,在你的controller中:
代码如下 复制代码
$objectphpexcel = new phpexcel();
$objectphpexcel->setactivesheetindex(0);
ob_end_clean();
ob_start();
header('content-type : application/vnd.ms-excel');
header('content-disposition:attachment;filename='.'xiaoqiang-'.date(ymj).'.xls');
$objwriter= phpexcel_iofactory::createwriter($objectphpexcel,'excel5');
$objwriter->save('php://output');
http://www.bkjia.com/phpjc/630690.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/630690.htmltecharticle文章来给大家介绍在yii框架中利用phpexcel插件来快速实例导出excel数据的具体方法,有在使用yii的同学不防进入参考一下。 最近在研究php的...
