本篇文章為大家展示了如何在Yii2框架中使用PHPExcel導(dǎo)出Excel文件,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
按需制作網(wǎng)站可以根據(jù)自己的需求進(jìn)行定制,成都做網(wǎng)站、網(wǎng)站建設(shè)構(gòu)思過(guò)程中功能建設(shè)理應(yīng)排到主要部位公司成都做網(wǎng)站、網(wǎng)站建設(shè)的運(yùn)用實(shí)際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實(shí)際意義最簡(jiǎn)單的利用composer安裝
composer require "phpoffice/phpexcel": "*"
如果沒(méi)有安裝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類里修改相應(yīng)的namespace也可。
2、按照下面的代碼修改PHPExcel代碼目錄里的Autoloader.php文件,對(duì)比源文件改成:
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; }
上面的函數(shù)中,注釋掉的是原有的代碼。
3、下面的代碼是輸出Excel,以及一些常用的屬性設(shè)置,在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; //報(bào)表頭的輸出 $objectPHPExcel->getActiveSheet()->mergeCells('B1:G1'); $objectPHPExcel->getActiveSheet()->setCellValue('B1','產(chǎn)品信息表'); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','產(chǎn)品信息表'); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','產(chǎn)品信息表'); $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.'頁(yè)'); $objectPHPExcel->setActiveSheetIndex(0)->getStyle('G2') ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); //表格頭的輸出 $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B3','編號(hào)'); $objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(6.5); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C3','名稱'); $objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(17); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D3','生產(chǎn)廠家'); $objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(22); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E3','單位'); $objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F3','單價(jià)'); $objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15); $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('G3','在庫(kù)數(shù)'); $objectPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15); //設(shè)置居中 $objectPHPExcel->getActiveSheet()->getStyle('B3:G3') ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //設(shè)置邊框 $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); //設(shè)置顏色 $objectPHPExcel->getActiveSheet()->getStyle('B3:G3')->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC'); } //明細(xì)的輸出 $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); //設(shè)置邊框 $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; } //設(shè)置分頁(yè)顯示 //$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="'.'產(chǎn)品信息表-'.date("Y年m月j日").'.xls"'); $objWriter= PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel5'); $objWriter->save('php://output');
上述內(nèi)容就是如何在Yii2框架中使用PHPExcel導(dǎo)出Excel文件,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享文章:如何在Yii2框架中使用PHPExcel導(dǎo)出Excel文件-創(chuàng)新互聯(lián)
標(biāo)題URL:http://jinyejixie.com/article42/djspec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿(mào)建站、全網(wǎng)營(yíng)銷推廣、小程序開(kāi)發(fā)、網(wǎng)站收錄、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容