Reputation: 265
I am currently using Maatwebsite's Excel package and am able to generate a single table nicely with columns and values that I want but I want to be able to generate tables, with other tables underneath within a single excel sheet. Is it possible?
The screenshot that is attached above is the excel file that I am able to generate for now and the screenshot below is an example of the excel file I want to generate, with tables underneath another table. Is there a way to do this within a single Excel Export class?
Upvotes: 4
Views: 9379
Reputation: 614
if you want to export your data in spreadsheet in desire format then you could grab the collection and send it to view and display as per your requirement. here is the docs to export as excel from view : https://docs.laravel-excel.com/3.0/exports/from-view.html Hope this Helps
Upvotes: 3
Reputation: 5149
I don't believe you are going to find a way to do this using just Laravel Excel — most people would create a new sheet for each dataset. But ultimately, you can format the data however you like if you build your output array manually.
Here's one way you could do it.
Controller
Pass in the arrays directly to the export class, instead of a collection.
$arrays = [$level_one_array, $level_two_array, $level_three_array];
return Excel::download(new YourExportClass($arrays), 'YourFilename.xlsx');
Export
Build an output array corresponding to your desired layout, then wrap it in a collection.
class YourExportClass implements FromCollection
{
use Exportable;
private $collection;
public function __construct($arrays)
{
$output = [];
foreach ($arrays as $array) {
// get headers for current dataset
$output[] = array_keys($array[0]);
// store values for each row
foreach ($array as $row) {
$output[] = array_values($row);
}
// add an empty row before the next dataset
$output[] = [''];
}
$this->collection = collect($output);
}
public function collection()
{
return $this->collection;
}
}
Upvotes: 2