如何使用 laravel 5.4 使用 Maatwebsite 在 excel 中设置列

how to set the columns in an excel using Maatwebsite using laravel 5.4

您好,我有这个下载的 csv。现在我的问题是我想为我的 excel 文件获取某些列,而不是将数据库中的所有字段都拉到 csv。现在我的下载 csv 工作正常并且可以下载数据。现在我希望只有某些列显示到我的 csv 文件中。 getCell 代码不起作用。这是我下面的代码

//download csv all pending
    public function downloadExcelAllPending($type){
        $data = BookingHistory::orderBy('checkin_date', 'desc')->get()->toArray();

        return Excel::create('booking_history', function($excel) use ($data) {

        $excel->sheet('mySheet', function($sheet) use ($data)
        {

            $sheet->getCell('A1')->setValue('first_name');
            $sheet->fromArray($data);


         });

        })->download($type);

现在 $sheet->getCell('A1')->setValue('first_name'); 这行代码将无法运行。有人可以帮我解决这个问题吗?非常感谢任何帮助。 TIA.

从数据库中获取数据时,您可以 select 只有所需的属性:

public function downloadExcelAllPending($type)
{
    $data = BookingHistory::orderBy('checkin_date', 'desc')
        ->select([
            'checkin_date',
            // your other attributes
        ])
        ->get()
        ->toArray();

    return Excel::create('booking_history', function ($excel) use ($data) {
        $excel->sheet('mySheet', function ($sheet) use ($data) {
            $sheet->fromArray($data);
        });
    })->download($type);
}