当前位置:首页 > CMS教程 > 其它CMS > 列表

Yii框架使用PHPExcel导出Excel文件的方法分析【改进版】

发布:smiling 来源: PHP粉丝网  添加日期:2021-12-07 16:14:34 浏览: 评论:0 

本文实例讲述了Yii框架使用PHPExcel导出Excel文件的方法,分享给大家供大家参考,具体如下:

最近在研究PHP的Yii框架,很喜欢,碰到导出Excel的问题,研究了一下,就有了下面的方法:

1、首先在config\main.php中添加对PHPExcel的引用,我的方式是这样:

  1. // autoloading model and component classes 
  2. 'import'=>array
  3.     /*'application.modules.srbac.controllers.SBaseController',*/ 
  4.     'application.models.*'
  5.     'application.components.*'
  6.     'application.extensions.phpexcel.*'
  7. ), 

另外也有人用components 这个配置,但是我的有问题,所以就用上面的方法。

2、按照下面的代码修改PHPExcel代码目录里的Autoloader.php文件:

  1. public static function Register() { 
  2.     /*if (function_exists('__autoload')) { 
  3.       //  Register any existing autoloader function with SPL, so we don't get any clashes 
  4.       spl_autoload_register('__autoload'); 
  5.     } 
  6.     //  Register ourselves with SPL 
  7.     return spl_autoload_register(array('PHPExcel_Autoloader', 'Load'));*/ 
  8.     $functions = spl_autoload_functions(); 
  9.       foreach ( $functions as $function
  10.         spl_autoload_unregister($function); 
  11.       $functions = array_merge(array(array('PHPExcel_Autoloader','Load')),$functions); 
  12.       foreach ( $functions as $function
  13.         $x = spl_autoload_register($function); 
  14.       return $x
  15. }  //  function Register() 

上面的函数中,注释掉的是原有的代码。

3、下面的代码是输出Excel,以及一些常用的属性设置,在你的controller中:

  1. /* 
  2. 导出为Excel 
  3. */ 
  4. public function actionExport() 
  5.     $objectPHPExcel = new PHPExcel(); 
  6.     $objectPHPExcel->setActiveSheetIndex(0); 
  7.     $page_size = 52; 
  8.     //数据的取出 
  9.     $model = Yii::app()->session['printdata']; 
  10.     $dataProvider = $model->search(); 
  11.     $dataProvider->setPagination(false); 
  12.     $data = $dataProvider->getData(); 
  13.     $count = $dataProvider->getTotalItemCount(); 
  14.     //总页数的算出 
  15.     $page_count = (int)($count/$page_size) +1; 
  16.     $current_page = 0; 
  17.     $n = 0; 
  18.     foreach ( $data as $product ) 
  19.     { 
  20.       if ( $n % $page_size === 0 ) 
  21.       { 
  22.         $current_page = $current_page +1; 
  23.         //报表头的输出 
  24.         $objectPHPExcel->getActiveSheet()->mergeCells('B1:G1'); 
  25.         $objectPHPExcel->getActiveSheet()->setCellValue('B1','产品信息表'); 
  26.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表'); 
  27.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','产品信息表'); 
  28.         $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getFont()->setSize(24); 
  29.         $objectPHPExcel->setActiveSheetIndex(0)->getStyle('B1'
  30.           ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  31.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','日期:'.date("Y年m月j日")); 
  32.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G2','第'.$current_page.'/'.$page_count.'页'); 
  33.         $objectPHPExcel->setActiveSheetIndex(0)->getStyle('G2'
  34.           ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
  35.         //表格头的输出 
  36.         $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5); 
  37.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B3','编号'); 
  38.         $objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(6.5); 
  39.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C3','名称'); 
  40.         $objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(17); 
  41.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D3','生产厂家'); 
  42.         $objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(22); 
  43.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E3','单位'); 
  44.         $objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15); 
  45.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F3','单价'); 
  46.         $objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15); 
  47.         $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G3','在库数'); 
  48.         $objectPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15); 
  49.         //设置居中 
  50.         $objectPHPExcel->getActiveSheet()->getStyle('B3:G3'
  51.           ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
  52.         //设置边框 
  53.         $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' ) 
  54.           ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  55.         $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' ) 
  56.           ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  57.         $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' ) 
  58.           ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  59.         $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' ) 
  60.           ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  61.         $objectPHPExcel->getActiveSheet()->getStyle('B3:G3' ) 
  62.           ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  63.         //设置颜色 
  64.         $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getFill() 
  65.           ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC'); 
  66.       } 
  67.       //明细的输出 
  68.       $objectPHPExcel->getActiveSheet()->setCellValue('B'.($n+4) ,$product->id); 
  69.       $objectPHPExcel->getActiveSheet()->setCellValue('C'.($n+4) ,$product->product_name); 
  70.       $objectPHPExcel->getActiveSheet()->setCellValue('D'.($n+4) ,$product->product_agent->name); 
  71.       $objectPHPExcel->getActiveSheet()->setCellValue('E'.($n+4) ,$product->unit); 
  72.       $objectPHPExcel->getActiveSheet()->setCellValue('F'.($n+4) ,$product->unit_price); 
  73.       $objectPHPExcel->getActiveSheet()->setCellValue('G'.($n+4) ,$product->library_count); 
  74.       //设置边框 
  75.       $currentRowNum = $n+4; 
  76.       $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum ) 
  77.           ->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  78.       $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum ) 
  79.           ->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  80.       $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum ) 
  81.           ->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  82.       $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum ) 
  83.           ->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  84.       $objectPHPExcel->getActiveSheet()->getStyle('B'.($n+4).':G'.$currentRowNum ) 
  85.           ->getBorders()->getVertical()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  86.       $n = $n +1; 
  87.     } 
  88.     //设置分页显示 
  89.     //$objectPHPExcel->getActiveSheet()->setBreak( 'I55' , PHPExcel_Worksheet::BREAK_ROW ); 
  90.     //$objectPHPExcel->getActiveSheet()->setBreak( 'I10' , PHPExcel_Worksheet::BREAK_COLUMN ); 
  91.     $objectPHPExcel->getActiveSheet()->getPageSetup()->setHorizontalCentered(true); 
  92.     $objectPHPExcel->getActiveSheet()->getPageSetup()->setVerticalCentered(false); 
  93.     ob_end_clean(); 
  94.     ob_start(); 
  95.     header('Content-Type : application/vnd.ms-excel'); 
  96.     header('Content-Disposition:attachment;filename="'.'产品信息表-'.date("Y年m月j日").'.xls"'); 
  97.     $objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5'); 
  98.     $objWriter->save('php://output'); 

代码执行后,会直接生成Excel,并提示下载或打开。

Tags: Yii PHPExcel Excel

分享到: