4

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?

Current Result

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?

Wanted result

2
  • in the collection() method of the export class, can you return a collection of collections? docs.laravel-excel.com/3.1/exports/collection.html Commented Aug 27, 2019 at 4:51
  • I think I am already returning a collection of collections this is my return collect method -> "return collect([ $level_one_array, $level_two_array, $level_three_array, ]);" but how do I have multiple tables in a single excel sheet with different headings and diff no of columns like the second screenshot I attached? Cuz I can't find anything similar in the documentation Commented Aug 27, 2019 at 5:17

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

I know it's 2 years later but just wanted to say for anyone else Googling that this works.
So you have to make multiple tables in the view?
@sdeav It does not matter. You can create as many as table you want. Its a view so you can append any thing over there.
2

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.