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');
        }

猜你喜欢

转载自blog.csdn.net/servicesYY/article/details/83302856