Przeglądaj źródła

fix sql to excel

GGican 9 lat temu
rodzic
commit
3565d0ba1e

+ 79 - 12
app/Http/Controllers/Admin/SqlController.php

xqd xqd xqd xqd
@@ -9,6 +9,8 @@ use Illuminate\Http\Request;
 use App\Http\Requests;
 use App\Http\Controllers\Controller;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Input;
+use Illuminate\Support\Facades\Storage;
 use Maatwebsite\Excel\Facades\Excel;
 
 class SqlController extends CommonController
@@ -21,28 +23,33 @@ class SqlController extends CommonController
 
     }
 
+    //备份导出sql为xls
     public function backups(Request $request)
     {
-        //生成备份文件
+        //生成备份文件
         $excle_path = date('YmdHis');
 
-        //数据库所有表
+        //数据库所有表
         $tables = DB::select('SHOW TABLES');
         $tables =  json_decode( json_encode( $tables),true);
         $table_name = array();
         foreach($tables as $k => $v){
             $table_name[] = $v['Tables_in_zcju'];
         }
-        //循环查表导出为excle文件
-        foreach($table_name as $value){
-            $data = DB::table($value)->get();
-            $data =  json_decode( json_encode( $data),true);
-            Excel::create($value,function($excle) use ($data){
-                $excle->sheet('score',function($sheet) use ($data){
-                    $sheet->rows($data);
+        //循环查表导出excle文件
+
+        Excel::create($excle_path.'_'.$request->all()['name'],function($excle) use ($table_name){
+            foreach($table_name as $value){
+                $data = DB::table($value)->get();
+                $data =  json_decode( json_encode( $data),true);
+
+                $excle->sheet($value,function($sheet) use ($data){
+                    $sheet->fromArray($data);
                 });
-            })->store('xls',storage_path('databases/'.$excle_path.'_'.$request->all()['name']));
-        }
+            }
+        })->store('xls',storage_path('databases/'));
+
+
         $this->mkFolder(storage_path('databases/'));
         //存datas表
         $input = $request->all();
@@ -50,17 +57,39 @@ class SqlController extends CommonController
         if(Data::create($input)){
             return redirect('/sql');
         }
-        return 1;
+        return back();
     }
 
+
+    // 恢复备份 xls导入到数据库
     public function recovery($id)
     {
+        $tables = DB::select('SHOW TABLES');
+        $tables =  json_decode( json_encode( $tables),true);
+        $table_name = array();
+        foreach($tables as $k => $v){
+            $table_name[] = $v['Tables_in_zcju'];
+        }
+
+        dump($table_name);
+        $path = Data::where('id','=',$id)->first()->path;
+        $filePath = 'storage/databases/'.$path.'/'.$table_name[1].'.xls';
+        Excel::load($filePath, function($reader) {
+            $data = $reader->toArray();
+            dd($data);
+        });
 
     }
 
+    //导出xls  下载链接
     public function download($id)
     {
+        $header = array('Content-Type'=>'application/x-xls');
+        $path = Data::find($id)->path;
 
+        $file = storage_path('databases/'.$path.'.xls').Input::get('file');
+        $file_name = date('YmdHis').'.xls';
+        return response()->download($file,$file_name,$header);
     }
 
 //    修改文件夹权限
@@ -76,3 +105,41 @@ class SqlController extends CommonController
     }
 
 }
+
+
+
+
+
+
+//    public function backups(Request $request)
+//    {
+//        //生成备份文件夹
+//        $excle_path = date('YmdHis');
+//
+//        //数据库所有表名
+//        $tables = DB::select('SHOW TABLES');
+//        $tables =  json_decode( json_encode( $tables),true);
+//        $table_name = array();
+//        foreach($tables as $k => $v){
+//            $table_name[] = $v['Tables_in_zcju'];
+//        }
+//        //循环查表导出为excle文件
+//        foreach($table_name as $value){
+//            $data = DB::table($value)->get();
+//            $data =  json_decode( json_encode( $data),true);
+//            Excel::create($value,function($excle) use ($data){
+//                $excle->sheet('score',function($sheet) use ($data){
+//                    $sheet->fromArray($data);
+//                });
+//            })->store('xls',storage_path('databases/'.$excle_path.'_'.$request->all()['name']));
+//        }
+//
+//        $this->mkFolder(storage_path('databases/'));
+//        //存datas表
+//        $input = $request->all();
+//        $input['path'] = $excle_path.'_'.$input['name'] ;
+//        if(Data::create($input)){
+//            return redirect('/sql');
+//        }
+//        return back();
+//    }

+ 1 - 1
app/Http/routes.php

xqd
@@ -29,7 +29,7 @@ Route::group(['namespace'=>'Admin','middleware'=>['admin.login']],function() {
     Route::get('/table_search/{id}','SearchController@table_search');
     //sql->excel->sql
     Route::get('/sql','SqlController@index');
-    Route::post('/sql/recovery/{id}','SqlController@recovery');
+    Route::get('/sql/recovery/{id}','SqlController@recovery');
     Route::post('/sql/backups','SqlController@backups');
     Route::get('/sql/download/{id}','SqlController@download');
 

+ 2 - 25
resources/views/admin/sql/index.blade.php

xqd xqd
@@ -54,8 +54,8 @@
                                             <td>{{ $value->description }}</td>
                                             <td>{{ $value->created_at }}</td>
                                             <td>
-                                                <a class=" btn-sm btn-success"><i class="fa  fa-cloud-download"></i> 导出</a>
-                                                <a class=" btn-sm btn-danger" onclick="recovery({{ $value->id }}})"><i class="fa fa-mail-reply"></i>&nbsp;恢复</a>
+                                                <a href="{{ url('/sql/download/'.$value->id) }}" class=" btn-sm btn-success"><i class="fa  fa-cloud-download"></i> 导出</a>
+                                                <a href="{{ url('/sql/recovery/'.$value->id) }}" class=" btn-sm btn-danger"><i class="fa fa-mail-reply"></i>&nbsp;恢复</a>
                                             </td>
                                         </tr>
                                         @endforeach
@@ -111,27 +111,4 @@
         </form>
     </div>
 
-    <script>
-        function recovery(data_id){
-            layer.confirm('确定要恢复这个备份吗?(备份将覆盖原数据库数据!)',{
-                btn:['确定','取消']
-            },function(){
-                $.post('{{ url('sql/recovery') }}/'+data_id,{
-                    '_method':'post',
-                    '_token' :'{{ csrf_token() }}'
-                },function(data){
-                    if(data.status == 1){
-                        location.href = location.href;
-                        layer.alert(data.msg, {icon: 6});
-                    }else {
-                        layer.alert(data.msg, {icon: 5});
-                    }
-                });
-            },function(){
-
-            });
-        }
-
-    </script>
-
     @endsection