Yii2框架中使用PHPExcel导出Excel文件的示例
程序员文章站
2024-03-11 14:43:19
最近在研究php的yii框架,很喜欢,碰到导出excel的问题,研究了一下,就有了下面的方法:
最简单的利用composer安装
com...
最近在研究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,并提示下载或打开。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读