Lumen5.5,使用laravel excel 下載 、導入excel文件


1、安裝

首先是安裝laravel excel,使用composer安裝

composer require maatwebsite/excel ~2.1.0

2、配置

在bootstrap/app.php中,加入如下配置:

//使用 laravel-excel
$app->register(Maatwebsite\Excel\ExcelServiceProvider::class);

 3、導入excel,定義好路由,上傳excel文件到后台,使用如下方法,可直接解析出excel的數據

$file =  $request->file('myfile');
//獲取數組類型的數據
$results = Excel::load($file)->get()->toArray();

到這里還沒完,你會發現導入的文件,信息獲取不完整,中文得不到,需要在

maatwebsite/excel/src/config/excel.php之下修改:

'to_ascii' =>true 改成 'to_ascii' => false,

 這個很坑。這樣導入就完成了。

-------------------------------------------------------------------------------------------------------------------------

下載Excel

1、可以直接在路由配置

Route::get('download',function(){
return response()->download(realpath(base_path('public')).'/student.xlsx', 'student.xlsx');
}); 

 然后在瀏覽器訪問這條路徑,就可以的下載了,也可封裝成方法:

public static function downLoad($fileName,$goalFileName='StudentExample'){
$path =realpath(base_path('public')).'/'.$goalFileName.'.xlsx';
return response()->download($path,$fileName.'.xlsx');
}

 然后可以使用js,打開窗口,下載文件

let url = APP.$Api.getExampleExcel();
window.open(url);
這里是在vue中的寫法, APP . $Api . getExampleExcel ()獲取后台地址

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM