laravel 使用EXCEL读写 maatwebsite/excel

1:首先在项目的composer.json 中添加 

 "maatwebsite/excel": "~2.1.0"   (根据自己框架选择版本)

2:接下来运行   composer update  命令

3:运行完成后再   app/config/app.php    配置文件中分别添加

  Maatwebsite\Excel\ExcelServiceProvider::class,

 'Excel' => Maatwebsite\Excel\Facades\Excel::class,

4:添加完成后运行

     php artisan vendor:publish

5:接下来就可以使用了

在最上面引入 

use Excel;

读取

        if(!$request->hasFile('file')){
            exit('上传文件为空!');
        }

        
        $file = $_FILES;
        $excel_file_path = $file['file']['tmp_name'];
        $res = [];
        Excel::load($excel_file_path, function($reader) use( &$res ) {
            $reader = $reader->getSheet(0);
            $res = $reader->toArray();
        });

        $num_add=0;
        for($i = 0;$i<count($res);$i++){
            
        }

  导出

        if(count($list) == 0){
            return '未知的号码';
        }else{
            $cellData = [
                ['号码','(日)下周期流量模组','(日)下周期语音模组','(月)近三月平均计费流量','(月)近三月平均账单费用(财务科目分摊后)','所属公司','时间'],
                ];
            foreach ($list as $data){
                $data = [$data->mobile,$data->flow,$data->voice,$data->trimester,$data->trimester_mean,$data->belong,$data->created_at];
                array_push($cellData,$data);
            }

            Excel::create($request->tel,function($excel) use ($cellData) {
                $excel->sheet('score', function($sheet) use ($cellData) {$sheet->rows($cellData);});
            })->export('xls');
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值