2017-08-15 132 views
0

我想更改列标题的名称。 你好吗?Laravel with Maatwebsite

这是在控制器代码:

public function excelterima($nm_perusahaan){ 
    if($user = Auth::user()->admin == 2){ 
     $users = Mstbeasiswa::select('NO_ANGGOTA', 'NM_ANGGOTA', 'GOLONGAN', 'NPK', 'CABANG', 'NM_ANAK', 'NM_SKL', 'id') 
       ->where('flag_terima', '1') 
       ->where('NM_PERUSAHAAN', $nm_perusahaan) 
       ->orderBy('id', 'asc') 
       ->get()->toArray(); 

     //work on the export 
     Excel::create($nm_perusahaan, function($excel) use ($users){ 
      $excel->sheet('sheet 1', function($sheet) use ($users) 
      { 
       $sheet->fromArray($users); 
       ob_end_clean(); 
      }); 
     })->download('xlsx'); 
     return redirect('/beasiswaditerima'); 
    }else{ 
     return redirect('/home'); 
    } 
} 

输出电流:

current OUTPUT

+0

如果需要,您可以在查询中使用'select ... AS'来实现此目的。 – Ohgodwhy

+0

谢谢:) ................. –

回答

1

默认情况下,您可以根据该LaravelExcelWorksheet实例的fromArray()方法自动生成的标题给定数组键。为了禁用此自动生成,您需要将false传递给第五个参数($headingGeneration)。下面是供您参考fromArray()方法签名:

public function fromArray($source = null, $nullValue = null, $startCell = 'A1', $strictNullComparison = false, $headingGeneration = true) 

可以使用row()方法来添加自定义标题。使用您的代码示例,现在的代码应该是这样的:

$users = Mstbeasiswa::select('NO_ANGGOTA', 'NM_ANGGOTA', 'GOLONGAN', 'NPK', 'CABANG', 'NM_ANAK', 'NM_SKL', 'id') 
    ->where('flag_terima', '1') 
    ->where('NM_PERUSAHAAN', $nm_perusahaan) 
    ->orderBy('id', 'asc') 
    ->get() 
    ->toArray(); 

Excel::create($nm_perusahaan, function ($excel) use ($users) { 
    $excel->sheet('sheet 1', function ($sheet) use ($users) { 
     // Set your custom header. 
     $sheet->row(1, ['COL1', 'COL2', 'COL3', 'COL4', 'COL5', 'COL6', 'COL7', 'COL8']); 

     // Set the data starting from cell A2 without heading auto-generation. 
     $sheet->fromArray($users, null, 'A2', false, false); 
    }); 
})->download('xlsx'); 

或者你其实可以只保留fromArray()方法调用,但后来随着row()方法,像这样更换自动生成的头:

$excel->sheet('sheet 1', function ($sheet) use ($users) { 
    $sheet->fromArray($users); 

    // Replace the header, but this should come after fromArray(). 
    $sheet->row(1, ['COL1', 'COL2', 'COL3', 'COL4', 'COL5', 'COL6', 'COL7', 'COL8']); 
}); 

希望得到这个帮助!