Lumen5.5,使用laravel excel 下载 、导入excel文件

1、安装

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

composer require maatwebsite/excel ~2.1.0

2、配置

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

//使用 laravel-excel
$app->register(MaatwebsiteExcelExcelServiceProvider::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()获取后台地址
记录点滴,迭代精进,追求新生。Email: 942298768@qq.com
原文地址:https://www.cnblogs.com/chaoyong/p/8206879.html