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)).'/'.$goalFileName.'.xlsx';return response()->download($path,$fileName.'.xlsx');}

 然后可以使用js,打开窗口,下载文件

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

 

转载于:https://www.cnblogs.com/chaoyong/p/8206879.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值