Search code examples
laravelmaatwebsite-excel

How to generate dynamic columns using Maatwebsite Excel in Laravel 5.1


I can generate rows dynamically like this

$row_count = 2;
for($i=0; $i < count($student_data); $i++) {
  $sheet->Row($row_count, array(
    $student_data[$i]->stud_number, 
    $student_data[$i]->first_name .' ' .$student_data[$i]->last_name 
  ));
  $row_count++;
}

$sheet->cells('A1:I'.$row_count, function($cells) {
  $cells->setAlignment('center');
}); 

But I can't generate column dynamically, I want to do something like this

$current_column = 'C';
for($i=0; $i < count($subject_data); $i++) {
  //I want here my column incremented by 1 like D, E, F, G....
  //Do something..
}

Solution

  • You can increment alphabetically too:

    $current_column = 'C';
    
    for($i=0; $i < count($subject_data); $i++) {
        print $current_column; // Will be C, D, E, etc...
        $current_column++; // Increment letter
    }