2017-08-15 102 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']); 
}); 

希望得到這個幫助!