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

问题描述 投票:1回答:1

我正在尝试导出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');
    }
laravel laravel-5 laravel-5.5 maatwebsite-excel
1个回答
3
投票

我们开工吧 :

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

        //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');
}
© www.soinside.com 2019 - 2024. All rights reserved.