Skip to content
You're reading the docs for Laravel Excel 3.1. The current version is Laravel Excel 4.x (upgrade guide).

From View ​

Exports can be created from a Blade view, by using the FromView concern.

php
namespace App\Exports;

use App\Invoice;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;

class InvoicesExport implements FromView
{
    public function view(): View
    {
        return view('exports.invoices', [
            'invoices' => Invoice::all()
        ]);
    }
}

It will convert an HTML table into an Excel spreadsheet. For example; invoices.blade.php:

html
<table>
    <thead>
    <tr>
        <th>Name</th>
        <th>Email</th>
    </tr>
    </thead>
    <tbody>
    @foreach($invoices as $invoice)
        <tr>
            <td>{{ $invoice->name }}</td>
            <td>{{ $invoice->email }}</td>
        </tr>
    @endforeach
    </tbody>
</table>

Attribute in tag html support:

AttributesDescription
bgcolorset background color to cell <td>
colspanmerge column cell
rowspanmerge row cell
widthset width to cell
heightset height to cell
data-formatset format PhpOffice\PhpSpreadsheet\Style::NumberFormat
data-typeset type PhpOffice\PhpSpreadsheet\Cell
alignset text align to cell
valignset vertical align to cell
styleset style to cell

Style inline support

  • background
  • background-color
  • color
  • border|border-top|border-bottom|border-left|border-right
  • font-size|font-weight|font-sytle|font-family
  • text-decoration: underline|line-through
  • text-align
  • vertical-align
  • width|height
  • word-wrap
  • text-indent

You can download the export in your controller:

php
public function export() 
{
    return Excel::download(new InvoicesExport, 'invoices.xlsx');
}