An alternative approach is to create an array during your first pass of the data. This array would contain arrays of records for each rep. EG
$salesdata = [
'Sales_rep1' => [
0 => [ 'Col1',
'Col2',
'Col3'
],
1 => [ 'Col1',
'Col2',
'Col3'
],
2 => [ 'Col1',
'Col2',
'Col3'
]
],
'Sales_rep2' => [
0 => [ 'Col1',
'Col2',
'Col3'
],
1 => [ 'Col1',
'Col2',
'Col3'
],
2 => [ 'Col1',
'Col2',
'Col3'
],
3 => [ 'Col1',
'Col2',
'Col3'
]
],
];
You can then loop through the array creating your sheets for each rep.
foreach ($salesdata as $rep => $sales) {
create new sheet
foreach ($sales as $recno => $recdata) {
add new row
foreach ($recdata as $col) {
output column
}
}
}