0

I am using laravel-excel.com in my Laravel project.

Since data is not coming from a specific model, I didn't want to create a dummy model class and I generate the excel file directly:

return (new \Illuminate\Support\Collection($data))->downloadExcel(
    'informe.xlsx',
    null,
    false
);

($data is a two-dimensional array with data of the unique data-sheet)

I wonder if there is any way to apply some style on columns (width, font-weight, etc.).
As far as I see it is possible ( like explained here) if I create a model for this excel.
Is it possible without?

guyaloni
  • 4,972
  • 5
  • 52
  • 92

1 Answers1

0

It looks like it is impossible to apply styles on excel sheet without having a model.

However, I found a trick that makes it very easy to move my code to a model, I leave it here in case it might help somebody.

The idea is to pass $data array (which I retrieve using a service) as a parameter to the constructor:

Controller:

return Excel::download(new MyExport($data), "$fileName.xlsx");

app/Exports/MyExport.php:

<?php

namespace App\Exports;

use \Illuminate\Support\Collection;

class ActuacionesExport implements FromCollection
{
    private $data;

    function __construct($data) {
        $this->data = $data;
    }

    public function collection()
    {
        return new Collection($this->data);
    }
}

As you can see, the model does not do anything except of returning a Collection instance, but it permits adding styling, column width, drawings - whatever you need in your excel!

guyaloni
  • 4,972
  • 5
  • 52
  • 92