Yii2 Framework to implement PHPEXCEL export Excel file Sharing method

Source: Internet
Author: User
This article mainly introduced the YII2 framework uses the Phpexcel to export the Excel file the related material, has the certain reference value, the interested small partner may refer to

Recently in the study of the YII framework of PHP, like, encountered the problem of exporting Excel, studied, there is the following method:

Easiest to install with composer


Composer require "Phpoffice/phpexcel": "*"

If the Conposer is not installed, refer to the following 1.2 steps

1. Introduction of Phpexcel

First you have to download Phpexcel

You can introduce it directly in the portal file index.php, or before you define the controller class, as long as it is introduced before you can use it.


Require DirName (dirname (__file__)). ' /excel/phpexcel.php ';

Or you can modify the corresponding namespace in the Phpexcel class.

2, according to the following code to modify the Phpexcel code directory in the autoloader.php file, compared to the source file changed to:


public static function Register () {  $functions = spl_autoload_functions ();  foreach ($functions as $function)    spl_autoload_unregister ($function);    $functions = Array_merge (Array (' Phpexcel_autoloader ', ' Load '), $functions);  foreach ($functions as $function)    $x = Spl_autoload_register ($function);    return $x;}

In the above function, the commented out is the original code.

3, the following code is the output of Excel, as well as some common property settings, in the 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;        The output of the report header $objectPHPExcel->getactivesheet ()->mergecells (' b1:g1 ');          $objectPHPExcel->getactivesheet ()->setcellvalue (' B1 ', ' Product information sheet ');        $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' B2 ', ' Product information sheet ');        $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' B2 ', ' Product information sheet ');        $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: '. Date ("Y-year M-month J-Day")); $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' G2 ', ' $current _page. '        /'. $page _count. ' Page '); $objectPHPExcel->setactivesheetindex (0)->getstyle (' G2 ')->getalignment ()->sethorizontal (phpexcel_                  Style_alignment::horizontal_right);        The output of the table header $objectPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (5);        $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' B3 ', ' numbered ');        $objectPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setwidth (6.5);        $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' C3 ', ' name ');        $objectPHPExcel->getactivesheet ()->getcolumndimension (' C ')->setwidth (17); $objectPHPExcel->setactivesheetindEX (0)->setcellvalue (' D3 ', ' manufacturer ');        $objectPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (22);        $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' E3 ', ' units ');        $objectPHPExcel->getactivesheet ()->getcolumndimension (' E ')->setwidth (15);        $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' F3 ', ' Unit price ');        $objectPHPExcel->getactivesheet ()->getcolumndimension (' F ')->setwidth (15);        $objectPHPExcel->setactivesheetindex (0)->setcellvalue (' G3 ', ' in the number of libraries ');                  $objectPHPExcel->getactivesheet ()->getcolumndimension (' G ')->setwidth (15); Set Center $objectPHPExcel->getactivesheet ()->getstyle (' B3:g3 ')->getalignment ()->sethorizontal (PH          Pexcel_style_alignment::horizontal_center); Set Border $objectPHPExcel->getactivesheet ()->getstyle (' B3:g3 ')->getborders ()->gettop ()->setb    Orderstyle (Phpexcel_style_border::border_thin);    $objectPHPExcel->getactivesheet ()->getstyle (' B3:g3 ')->getborders ()->getleft ()->setborderstyl        E (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 ()->setbordersty          Le (Phpexcel_style_border::border_thin); Set color $objectPHPExcel->getactivesheet ()->getstyle (' B3:g3 ')->getfill ()->setfilltype (phpexcel_                Style_fill::fill_solid)->getstartcolor ()->setargb (' FF66CCCC '); }//Detail output $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);      Set Border $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_thi      N); $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_t      HIN); $objectPHPExcel->getactivesheet ()->getstyle (' B '. ( $n +4). ': G '. $currentRowNum)->getborders ()->getvertical ()->setborderstyle (Phpexcel_style_border::border      _thin);      $n = $n +1;    }//Set paging display//$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= "'. ' Product Information Sheet-'. Date ("Y-year M-month J-Day").    XLS "');    $objWriter = Phpexcel_iofactory::createwriter ($objectPHPExcel, ' Excel5 '); $objWriter->save (' php://output ');

After the

code executes, Excel is generated directly and prompts to be downloaded or opened.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.