您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用php的lavarel框架導(dǎo)出文件”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
一、導(dǎo)出文件
1.創(chuàng)建一個(gè)干凈的控制器 ExcelController.php :
php artisan make:controller ExcelController --plain
2.然后在 routes.php 中定義相關(guān)路由:
Route::get('excel/export','ExcelController@export'); Route::get('excel/import','ExcelController@import');
3.接下來在 ExcelController.php 中定義 export 方法實(shí)現(xiàn)導(dǎo)出功能:
<?php namespace App\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller as BaseController; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use PHPExcel;//引入excel類 use IOFactory; use DB; class ExcelController extends Controller { //導(dǎo)出 public function Excel(){ $query=DB::table('change')->get();//查詢表 //print_r($query); if(!$query) return false;//判斷是否為空值 $obj=new PHPExcel();//實(shí)例化excel類 include_once('../app/libs/PhpExcel/PHPExcel/IOFactory.php');//引入IOFactory.php $obj->getProperties()-> setTitle("export") ->setDescription("none"); $obj-> setActiveSheetIndex(0); $fields = DB::select("SHOW COLUMNS FROM `change`");//查詢goods表中所有列名 //print_r($fields);die; $col = 0;//定義列 foreach($fields as $field){ $field =$field['Field']; $obj-> getActiveSheet() -> setCellValueByColumnAndRow($col, 1,$field); $col++; } $row = 2;//定義行 foreach($query as $data) { $col =0; foreach($fields as $field) { //print_r($data); $field =$field['Field']; $obj->getActiveSheet()->setCellValueByColumnAndRow($col,$row,!empty($data["$field"])?$data["$field"]:''); $col++; } $row++; } $obj-> setActiveSheetIndex(0); $objWriter =IOFactory :: createWriter($obj, 'Excel5'); header('Content-Type:application/vnd.ms-excel'); header('Content-Disposition:attachment;filename="Brand_' .date('Y-m-d') . '.xls"'); header('Cache-Control:max-age=0'); $objWriter-> save('php://output'); } }
“怎么用php的lavarel框架導(dǎo)出文件”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。