← Back to list

Exporting Styled Excel (XLSX) Files from HTML Tables in Laravel

Learn How to Generate Beautiful Excel Files Using Blade Views and Maatwebsite\Excel

Jamal Derdiwala · 2025-06-10 03:46 · 0 claps · 2.0 min read
#php #laravel #xlsx #export #filé
Open on Medium ↗
Wiki topics: 🌐 · Web Development 👗 · Fashion

Exporting Styled Excel (XLSX) Files from HTML Tables in Laravel

Learn How to Generate Beautiful Excel Files Using Blade Views and Maatwebsite\Excel

Exporting data to Excel is a common requirement in Laravel applications — especially for admin dashboards, reports, and data summaries. But what if you want your Excel export to retain HTML styles, such as background colors, text formatting, or layout?

In this guide, I’ll show you how to export a styled HTML table to an .xlsx file using the popular [Maatwebsite\Excel](https://laravel-excel.com/) package in Laravel.

And yes — it supports HTML-based styling!

Step 1: Install the Package

Start by installing the Laravel Excel package via Composer:

composer require maatwebsite/excel

Step 2: Create the Export Controller

Create a controller named LocationExportController with a method to trigger the export.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Exports\LocationsExport;
use Maatwebsite\Excel\Facades\Excel;

class LocationExportController extends Controller
{
    public function export()
    {
        $locations = [
            [
                'id' => 1,
                'name' => 'Head Office',
                'address' => '123 Main Street',
                'city' => 'Mumbai',
                'created_at' => now()->toDateTimeString(),
            ],
            [
                'id' => 2,
                'name' => 'Branch Office',
                'address' => '456 Park Avenue',
                'city' => 'Delhi',
                'created_at' => now()->toDateTimeString(),
            ]
        ];

        return Excel::download(new LocationsExport($locations), 'locations.xlsx');
    }
}

Step 3: Create the Export Class

Create the LocationsExport class inside App\Exports.

<?php

namespace App\Exports;

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

class LocationsExport implements FromView
{
    protected $locations;

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

    public function view(): View
    {
        return view('exports.locations', [
            'locations' => $this->locations
        ]);
    }
}

We’re using the FromView concern to generate Excel content directly from a Blade view.

Step 4: Create the Blade View

(resources/views/exports/locations.blade.php)

This view will contain a standard HTML table with inline styling, which gets translated into Excel formatting.

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th style="background-color: #4CAF50; color: white;">Name</th>
            <th>Address</th>
            <th>City</th>
            <th>Created At</th>
        </tr>
    </thead>
    <tbody>
        @foreach($locations as $location)
            <tr>
                <td style="background-color: red;">{{ $location['id'] }}</td>
                <td>{{ $location['name'] }}</td>
                <td>{{ $location['address'] }}</td>
                <td style="background-color: yellow;">{{ $location['city'] }}</td>
                <td>{{ $location['created_at'] }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

💡 Pro Tip: Excel supports a wide range of inline CSS styles when exporting from Blade views via Maatwebsite\Excel.

Step 5: Add a Route

In your web.php, add a route to test the export:

Route::get('/export-locations', [LocationExportController::class, 'export']);

Visit /export-locations in your browser, and an Excel file named locations.xlsx will be downloaded—complete with your custom styling.

Summary

You’ve now learned how to:

  • Export data to .xlsx from Laravel
  • Use HTML tables and Blade views for styled exports
  • Apply inline CSS for better visual formatting in Excel

This method is perfect when you want quick, customizable, and styled data exports without diving into Excel’s low-level APIs.

If you enjoyed this guide, follow me for more Laravel tips, tutorials, and best practices.

👉 Connect with me on LinkedIn

Thanks for reading!


메타데이터
post_id
a65588d2a2ee
slug
exporting-styled-excel-xlsx-files-from-html-tables-in-laravel-a65588d2a2ee
url
https://medium.com/@derdiwalajamal/exporting-styled-excel-xlsx-files-from-html-tables-in-laravel-a65588d2a2ee
canonical_url
https://medium.com/@derdiwalajamal/exporting-styled-excel-xlsx-files-from-html-tables-in-laravel-a65588d2a2ee
author_url
https://medium.com/@derdiwalajamal
status
ok
fetched_at
2026-06-25 16:53:31