Laravel Excel文件的导入和导出

//上传文件

    public function import(Request $request){

        if(Input::method() == 'POST'){

            // $filePath = '/storge/exports/'.iconv('UTF-8', 'GBK', '数据').'.xls';

            if(!($request->hasFile('file') && $request->file('file')->isValid())){

                return "文件不存在";

            }else{

                $file = Input::file('file');

                // dd($file);

                //获取临时文件的绝对路径

                $originalName = $file->getClientOriginalName(); // 文件原名

                // dd($originalName);

                $realPath = $file->getRealPath();

                // dd($realPath);

                // dd(iconv('gbk','utf-8',$originalName));

                // $filePath = "F:\PhpStudy\PHPTutorial\WWW\matchProject\storage\app\upload\\".iconv('UTF-8','GBK',date('Y-m-d').rand(0,999999999)).'.xls';

                // dd($filePath);

                // move_uploaded_file($realPath,$filePath );

                // dd();


                Excel::load($realPath, function($reader) use($file){

                //注意Excel 表的第一行不需要写入数据表

                $data = $reader->getSheet(0)->toArray();

                //dd($data);die;

                // $data = iconv('UTF-8','GBK',file_get_contents($filePath));

                // die;

                // $tmp = [];

                foreach($data as $key => $value){

                    if($key !=0){

                        // $temp[$key]['id'] = $value[0];不需要传递id,因为它会自动增长

                        $temp[$key]['mt_id'] = $value[1];

                        $temp[$key]['jushu'] = $value[2];

                        $temp[$key]['getscore'] = $value[3];

                        $temp[$key]['lostscore'] = $value[4];

                        $temp[$key]['get_push_ball'] = $value[5];

                        $temp[$key]['pai_count'] = $value[6];

                        $temp[$key]['style'] = $value[7];

                        $temp[$key]['result'] = $value[8];

                        $temp[$key]['sport_id'] = $value[9];                        

                    }

                }   


                //写入数据表

                    $result = DB::table('matchdetails')->insert($temp);

                    //判断

                    $response = $result ? '成功':'失败';

                    //输出信息

                    echo $response; 


                });

            }

        }else{

            $data = Matchdetails::get();

            // dd($data);

            return view('admin.excel.import',compact('data'));

        }

    }



   //下载文件

    public function export(){

        if(Input::method() === 'GET'){

            //查询比赛数据信息

                $data = Matchdetails::get();

                // dd($data);

                $cellData = [

                            ['序号','比赛项目id','局数','得分','失分','发接轮次','拍数','手段','得失分'],  

                        ];

                //循环数据

                foreach($data as $key => $val){

                    $cellData[]=[

                        $val->id,

                        $val->mt_id,

                        $val->jushu,

                        $val->getscore,

                        $val->lostscore,

                        $val->get_piush_ball,

                        $val->pai_count,

                        $val->style,

                        $val->result

                    ];

                }

                // dd($cellData);

                Excel::create('比赛数据导出',function ($excel) use ($cellData){

                        $excel->sheet('个人比赛数据信息', function ($sheet) use ($cellData){

                            $sheet->rows($cellData);

                    });//导出文件

            })->store('xls')->export('xls');

        }

    }

恩恩 ,大概就是这样

你可能感兴趣的:(Laravel Excel文件的导入和导出)