Yii框架使用PHPExcel导出Excel文件的方法分析【改进版】
发布:smiling 来源: PHP粉丝网 添加日期:2021-12-07 16:14:34 浏览: 评论:0
本文实例讲述了Yii框架使用PHPExcel导出Excel文件的方法,分享给大家供大家参考,具体如下:
最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法:
1、首先在config\main.php中添加对PHPExcel的引用,我的方式是这样:
- // autoloading model and component classes
- 'import'=>array(
- /*'application.modules.srbac.controllers.SBaseController',*/
- 'application.models.*',
- 'application.components.*',
- 'application.extensions.phpexcel.*',
- ),
另外也有人用components 这个配置,但是我的有问题,所以就用上面的方法。
2、按照下面的代码修改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()
上面的函数中,注释掉的是原有的代码。
3、下面的代码是输出Excel,以及一些常用的属性设置,在你的controller中:
- /*
- 导出为Excel
- */
- public function actionExport()
- {
- $objectPHPExcel = new PHPExcel();
- $objectPHPExcel->setActiveSheetIndex(0);
- $page_size = 52;
- //数据的取出
- $model = Yii::app()->session['printdata'];
- $dataProvider = $model->search();
- $dataProvider->setPagination(false);
- $data = $dataProvider->getData();
- $count = $dataProvider->getTotalItemCount();
- //总页数的算出
- $page_count = (int)($count/$page_size) +1;
- $current_page = 0;
- $n = 0;
- foreach ( $data as $product )
- {
- if ( $n % $page_size === 0 )
- {
- $current_page = $current_page +1;
- //报表头的输出
- $objectPHPExcel->getActiveSheet()->mergeCells('B1:G1');
- $objectPHPExcel->getActiveSheet()->setCellValue('B1','产品信息表');
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表');
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表');
- $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getFont()->setSize(24);
- $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')
- ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','日期:'.date("Y年m月j日"));
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G2','第'.$current_page.'/'.$page_count.'页');
- $objectPHPExcel->setActiveSheetIndex(0)->getStyle('G2')
- ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
- //表格头的输出
- $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B3','编号');
- $objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(6.5);
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C3','名称');
- $objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(17);
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D3','生产厂家');
- $objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E3','单位');
- $objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F3','单价');
- $objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
- $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G3','在库数');
- $objectPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
- //设置居中
- $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')
- ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //设置边框
- $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
- ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
- ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
- ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
- ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' )
- ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- //设置颜色
- $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getFill()
- ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC');
- }
- //明细的输出
- $objectPHPExcel->getActiveSheet()->setCellValue('B'.($n+4) ,$product->id);
- $objectPHPExcel->getActiveSheet()->setCellValue('C'.($n+4) ,$product->product_name);
- $objectPHPExcel->getActiveSheet()->setCellValue('D'.($n+4) ,$product->product_agent->name);
- $objectPHPExcel->getActiveSheet()->setCellValue('E'.($n+4) ,$product->unit);
- $objectPHPExcel->getActiveSheet()->setCellValue('F'.($n+4) ,$product->unit_price);
- $objectPHPExcel->getActiveSheet()->setCellValue('G'.($n+4) ,$product->library_count);
- //设置边框
- $currentRowNum = $n+4;
- $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
- ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
- ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
- ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
- ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum )
- ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- $n = $n +1;
- }
- //设置分页显示
- //$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , PHPExcel_Worksheet::BREAK_ROW );
- //$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , PHPExcel_Worksheet::BREAK_COLUMN );
- $objectPHPExcel->getActiveSheet()->getPageSetup()->setHorizontalCentered(true);
- $objectPHPExcel->getActiveSheet()->getPageSetup()->setVerticalCentered(false);
- ob_end_clean();
- ob_start();
- header('Content-Type : application/vnd.ms-excel');
- header('Content-Disposition:attachment;filename="'.'产品信息表-'.date("Y年m月j日").'.xls"');
- $objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5');
- $objWriter->save('php://output');
- }
代码执行后,会直接生成Excel,并提示下载或打开。
Tags: Yii PHPExcel Excel
相关文章
- ·Yii中单独为module加载Bootstrap或其他组件的4种方法(2014-01-07)
- ·yii2超好用的日期组件和时间组件(2019-07-30)
- ·详解PHP的Yii框架中扩展的安装与使用(2019-10-20)
- ·PHP的Yii框架中过滤器相关的使用总结(2019-10-31)
- ·简介PHP的Yii框架中缓存的一些高级用法(2019-10-31)
- ·深入解析PHP的Yii框架中的缓存功能(2019-11-05)
- ·PHP的Yii框架中创建视图和渲染视图的方法详解(2019-11-05)
- ·PHP的Yii框架中Model模型的学习教程(2019-11-05)
- ·详解PHP的Yii框架中的Controller控制器(2019-11-05)
- ·yii2的ActiveForm表单使用的方法介绍(2020-02-15)
- ·Yii2框架的csrf验证原理分析及token缓存解决方案(2020-04-05)
- ·Yii操作数据库的3种方法(2020-10-26)
- ·yii上传文件或图片实例(2020-11-05)
- ·yii框架表单模型使用及以数组形式提交表单数据示例(2020-11-25)
- ·yii框架通过控制台命令创建定时任务示例(2020-11-25)
- ·yii框架配置默认controller和action示例(2020-11-25)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)