[Vuejs]-Import an excel and insert into database

0👍

You could use Laravel-Excel to read the file, create an object or array and store the data on the table that you want. In the view (blade, vue, jQuery, whatever) you just must send the file as POST though a route like this:

Route::post('/importExcel', 'YourController@importExcelmethod');

In the controller you could make something like this:

public function importExcel(Request $request)
{
    if($request->file('import_file')){
        $path = $request->file('import_file')->getRealPath();
        $data = Excel::selectSheetsByIndex(0)->load($path, function($reader) {
        })->get()->toArray();
        if(!empty($data) && (count($data)> 0)){
            foreach ($data as $key => $value) {
                DB::table('your_table')->insert(
                    [
                        "a_column" => $value[0],
                        "another_comun" => $value[1],
                        //etc
                    ]
                );
            }

More info here and examples here: https://docs.laravel-excel.com/3.1/imports/

Actually there are some easier examples on how to store data to a Model from the xls like this:

public function import() 
{
    Excel::import(new UsersImport, 'users.xlsx');

    return redirect('/')->with('success', 'All good!');
}

Leave a comment