导出到Excel时出现非法偏移类型错误

时间:2018-01-23 02:53:32

标签: laravel laravel-5 laravel-5.5 maatwebsite-excel

我试图导出excel但是当我导出它时,它给出了一个非法偏移类型错误的错误。我怎样才能使它工作? 继承我的UsersController中的代码。

public function userExport()
{
$users = $this->users->paginate(
            $perPage = 20,
            Input::get('search'),
            Input::get('status'),
            Input::get('emp_status'),
            Input::get('level'),
            Input::get('age'),
            Input::get('gender'),
            Input::get('civil_status'),
            Input::get('role_id'),
            Input::get('birthmonth'),
            Input::get('company'),
            Input::get('branches'),
            Input::get('benefit'),
            Input::get('designation'),
            Input::get('tenure')
            // Input::get('gender')
        );
        return Excel::create('data_function',function($excel) use ($users){
            $excel->sheet('mysheet', function($sheet) use ($users){
                $sheet->fromArray($users);
            });
        })->download('xls');
    }

1 个答案:

答案 0 :(得分:2)

让我们这样做:

这对我有用,所以我试试了你,请检查你是否需要改变。

        //Initialize the array which will be passed into the Excel generator

    $userarray= [];

        // Define the Excel spreadsheet headers

    $userarray[] = ['id', 'search','status','any_thing u_want'];

        // Convert each member of the returned collection into an array,

       // and append it to the payments array.

     foreach ($users as $user) {
       $userarray[] = $user->toArray();
     }

     // Generate and return the spreadsheet

    Excel::create('payments', function($excel) use ($userarray) {

    // Set the spreadsheet title, creator, and description

    $excel->setTitle('users');
    $excel->setCreator('Laravel')->setCompany('any_name, LLC');
    $excel->setDescription('info file');

    // Build the spreadsheet, passing in the payments array

    $excel->sheet('sheet1', function($sheet) use ($userarray) {
        $sheet->fromArray($userarray, null, 'A1', false, false);
    });

})->download('xlsx');
}