如何?Yii2架構中使用PHPExcel匯出Excel檔案

來源:互聯網
上載者:User
本篇文章主要介紹了Yii2架構中使用PHPExcel匯出Excel檔案的相關資料,具有一定的參考價值,感興趣的小夥伴們可以參考一下

最近在研究PHP的Yii架構,很喜歡,碰到匯出Excel的問題,研究了一下,就有了下面的方法:

最簡單的利用composer安裝

composer require "phpoffice/phpexcel": "*"

如果沒有安裝conposer可以參考下面1.2步

1、引入PHPExcel

首先得要下載phpexcel地址:https://github.com/PHPOffice/PHPExcel/archive/1.8.1.zip

你可以直接在入口檔案index.php中引入,也可以在你定義的controller類之前,只要是在你使用之前引入就可以

require dirname(dirname(__FILE__)).'/excel/PHPExcel.php';

或者在phpexcel類裡修改相應的namespace也可。

2、按照下面的代碼修改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;}

上面的函數中,注釋掉的是原有的代碼。

3、下面的代碼是輸出Excel,以及一些常用的屬性設定,在controller中:

public function actionExport(){    $objectPHPExcel = new PHPExcel();    $objectPHPExcel->setActiveSheetIndex(0);      $page_size = 52;    $model = new NewsSearch();    $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,並提示下載或開啟。

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.