How to pre-format a cell to date on Laravel Excel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Pre-format Cells to Date in Laravel Excel

When exporting data from a Laravel application using packages like maatwebsite/excel, ensuring that complex data types, such as dates, are presented correctly in the resulting spreadsheet (XLS or XLSX) is crucial for usability. Simply pushing a standard PHP DateTime object into a cell might result in a generic "General" format, which is often insufficient for date representation.

This guide will walk you through the correct methodology to pre-format cells as dates when using Laravel Excel, focusing on robust solutions within your PHP and Laravel environment.

The Challenge: Data vs. Presentation

The core confusion often lies between the data value stored in your database/model and the presentation format applied to the spreadsheet cell. Your Eloquent model likely stores a standard datetime or timestamp, but the Excel export needs a specific string format (e.g., YYYY-MM-DD).

The maatwebsite/excel package relies on underlying libraries (like PhpSpreadsheet) to handle the actual file generation. To control cell formatting, you must explicitly tell the exporter how to render that data.

Solution 1: Formatting Data in PHP Before Exporting

The most reliable approach is to ensure the data being passed to the Excel writer is already in the exact string format you desire. This shifts the responsibility for formatting from the final export step to your application logic, which aligns well with clean architectural principles often emphasized in frameworks like Laravel.

If you are retrieving a date from your database, use PHP's date() function to convert the timestamp into the desired string format before writing it to the collection.

use Maatwebsite\Excel\Facades\Excel;
use App\Models\Event;
use Illuminate\Support\Facades\DB;

// Retrieve data
$events = Event::all();

$dataToExport = $events->map(function ($event) {
    // Assuming $event->start_date is a standard SQL DATETIME field
    $formattedDate = \Carbon\Carbon::parse($event->start_date)->format('Y-m-d');
    
    return [
        $event->id,
        $event->title,
        $formattedDate, // This is the formatted string we want in Excel
        $event->description
    ];
});

Excel::store($dataToExport, 'events_report.xlsx');

By using Carbon (which Laravel heavily integrates), you ensure your date manipulation is robust and timezone-aware, preventing common errors associated with raw PHP date handling. This practice of manipulating data before persistence or export is a fundamental concept in efficient application design, much like the principles discussed in modern frameworks like those promoted by laravelcompany.com.

Solution 2: Applying Cell Styles via Laravel Excel

While pre-formatting the value solves the content issue, you can further enhance the presentation by applying explicit styles to the cells. The maatwebsite/excel package allows you to define styles during the export process using custom collections or stream operations.

If you need specific cell formatting (like setting a number format code for dates), you often interact directly with the underlying PhpSpreadsheet object, which Laravel Excel exposes through its capabilities. For complex styling, it is best practice to leverage the package's ability to handle style definitions, ensuring consistency across all exported files.

Though direct manipulation of the cell format string (like setting numberFormat: 'yyyy-mm-dd') often requires deeper interaction with the underlying library objects, preparing the data as a perfectly formatted string first is the most transferable and stable method for large-scale exports.

Conclusion

To successfully pre-format cells to date in Laravel Excel, focus on data preparation. Do not rely solely on the export package to guess the required formatting. By using powerful tools like Carbon within your PHP logic to convert database timestamps into standardized string formats (Y-m-d), you ensure that your exported files are accurate, consistent, and immediately readable by end-users. This approach keeps your data integrity high while providing a smooth developer experience when dealing with complex file generation tasks.