• 一朝春雷满地新绿 习近平最牵挂的绿色故事这样写就 2019-08-23
  • 泰达官方宣布国安飞翼加盟 他曾战恒大一场扬名 2019-08-08
  • 王式刚的专栏作者中国国家地理网 2019-08-02
  • 2018俄罗斯世界杯揭幕战看点全揭秘 2019-08-02
  • 當前位置:天津赖子麻将手机版-建站教程-網站開發-PHP教程-Yii框架使用PHPExcel導出Excel文件的方法分析【改進版】

    咸宁晃晃红中赖子麻将:Yii框架使用PHPExcel導出Excel文件的方法分析【改進版】

    文章TAG:phpExcel yii
    時間:2019-07-24來源/作者:Cure 編輯:源碼庫 文章熱度:

    天津赖子麻将手机版 www.jiao120.com 本文實例講述了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,并提示下載或打開。

    希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。


    注:相關教程知識閱讀請移步到PHP教程頻道。
    相關PHP教程
    熱門標簽

    PHP教程Rss訂閱PHP教程搜索

  • 一朝春雷满地新绿 习近平最牵挂的绿色故事这样写就 2019-08-23
  • 泰达官方宣布国安飞翼加盟 他曾战恒大一场扬名 2019-08-08
  • 王式刚的专栏作者中国国家地理网 2019-08-02
  • 2018俄罗斯世界杯揭幕战看点全揭秘 2019-08-02
  • 江苏快三稳赚方法 二八杠有多少种生死门 斗牛怎么玩法算法 3d18年连线走势图 时时彩码倍投方案 北京pk赛车官方app下载 彩吧助手app下载 重庆时时彩是合法的吗? 时时彩万能六码 明珠自动投注系统 重庆时时最新开奖结果 3d组六一胆拖九投注表 福彩中豹子 ag假的不能再假 重庆时时彩定位胆推荐 腾讯分分彩计划 精准版