Laravel Excel table styles not applied to spreadsheet

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel Excel: How to Apply Custom Styles to Your Spreadsheet Data

As a senior developer working with data export tools like Laravel Excel, you often run into a common roadblock: wanting to maintain rich visual styling in your application's view while exporting that data to a spreadsheet. You've built complex HTML structures—like the table layouts you provided—but when you use Laravel Excel to export the resulting data, all those beautiful CSS styles disappear, leaving you with a plain, unformatted spreadsheet.

This post dives deep into why this happens and provides the correct, developer-focused strategies for achieving styled exports using Laravel Excel.

The Core Misunderstanding: Data vs. Presentation

The fundamental issue lies in how spreadsheet export libraries operate. Tools like Laravel Excel are designed to serialize data (arrays, collections of values) into a structured format (like CSV or XLSX). They do not inherently parse and render arbitrary HTML structure directly into cell formatting.

When you output raw HTML tables within your Blade view, Laravel Excel sees this as inert content that it cannot map onto its predefined row-and-column structure. The export process focuses solely on the underlying data values ($score->child->full_name, $indicator_star) and ignores the surrounding structural tags (<table>, <tr>, <td>).

To get styles into your spreadsheet, you must stop thinking about rendering HTML in the view and start thinking about structuring your data for export.

Strategy 1: Structuring Data for Export (The Recommended Path)

The most robust and maintainable way to handle exports is to prepare your data as a clean, structured array or collection before passing it to the exporter. This allows you to define the column headers and cell values explicitly, which the export library can then format correctly.

Instead of relying on raw HTML for structure, you should generate the necessary data points in your controller or service layer.

Consider restructuring your data so that each row corresponds directly to a spreadsheet row, and the data within that row defines the content for specific cells.

Here is an example of how you might structure the output data before exporting:

// Example Data Preparation (In your Controller)
$exportData = [];
foreach ($scores as $score) {
    // Assume you have pre-processed indicators into a simple array format
    $row = [
        $score->child->full_name, // Column 1: Name
        // ... other data points
        json_encode($score->scores), // We can put complex JSON/data in one cell if needed
    ];
    $exportData[] = $row;
}

// Now use the exporter on this clean data structure.
// Use the methods provided by Laravel Excel, such as ToExcel::fromArray()
use Maatwebsite\Excel\Facades\Excel;

Excel::make('report', $exportData)->download('report.xlsx');

By structuring your output this way, you gain complete control over what appears in the spreadsheet. You can use custom formatting features within the Excel file itself (like conditional formatting for numbers) instead of trying to force CSS via the export process. This aligns with good architectural practices, emphasizing clean separation between presentation logic and data handling, which is a core principle of modern Laravel development.

Strategy 2: Advanced Customization via Events (For Fine-Grained Control)

You mentioned looking into extending the package via events. While the standard view or query methods focus on data mapping, extending the underlying mechanism using events grants you access to hooks during the export process. This is exactly where you gain control over how custom objects are serialized.

As noted in the Laravel Excel documentation regarding extending functionality:

You are able to hook into the parent package by using events. No need to use convenience methods like "query" or "view", if you need full control over the export.

If you absolutely require complex, nested data from your Eloquent models to be rendered into specific cell formats (e.g., conditional colors based on a value), hooking into the export lifecycle allows you to inject custom logic that transforms your PHP objects into the exact string or formatted value required by the XLSX writer. This is an advanced technique best reserved for highly customized reporting needs, ensuring that even complex data structures are exported meaningfully, adhering to principles of clean code and maintainability advocated by the Laravel team.

Conclusion

In summary, you cannot directly map arbitrary HTML table styles from a Blade view into an Excel file using standard Laravel Excel methods because the tool focuses on data serialization, not HTML rendering. The solution is twofold: first, structure your data cleanly in PHP arrays before export, and second, if deep customization of cell content is needed, leverage the event system to hook into the package's internal workings. Focus on transforming your application state into structured data; that is the path to building scalable and reliable data exports.