How can I set text align right in the column on the laravel Excel maatwebsite?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Dynamic Formatting in Laravel Excel: Aligning Text Across Dynamic Ranges
As developers working within the Laravel ecosystem, we frequently use powerful packages like Maatwebsite\Excel to handle complex data exports. While these tools simplify data migration and reporting, sometimes we run into limitations when trying to apply granular formatting—especially when dealing with dynamic datasets where the row count changes constantly.
A common scenario arises when attempting to style entire columns in an exported spreadsheet. As you discovered, simply defining a static range like 'C2:C1000' fails when your data volume fluctuates, leading to incomplete or incorrect formatting for larger or smaller exports.
This post will dive deep into why this happens and provide a robust, dynamic solution using the underlying PhpSpreadsheet event system. We will learn how to apply text alignment to an entire column dynamically, ensuring your Excel exports are always accurate, regardless of whether you export 100 records or 10,000.
The Limitation of Static Ranges in Spreadsheet Exports
The core issue stems from how the underlying library, PhpSpreadsheet, processes worksheet styling via methods like sheetStyleCells(). When you specify a range (e.g., 'C2:C1000'), the library applies that style only to those exact cells within the defined scope. If your data set exceeds this limit, the rest of the column remains unstyled by that specific command.
This limitation forces us to rethink how we interact with the export process. We need a mechanism that can inspect the actual populated data after it has been written to the sheet and then apply styling based on that runtime information. This requires leveraging the event hooks provided by the package rather than relying solely on static range definitions.
The Dynamic Solution: Utilizing AfterSheet Events
The key to solving dynamic formatting problems lies in utilizing the events exposed by Laravel Excel, specifically the AfterSheet event. This event fires after all data has been written to the worksheet, giving us the opportunity to inspect the sheet object and calculate the necessary range dynamically.
Instead of hardcoding row numbers, we can determine the final row index based on the actual number of records present in our exported dataset.
Step-by-Step Implementation
To achieve dynamic right alignment for an entire column (let's assume Column C) across all rows:
- Determine the Maximum Row: Before applying the style, we need to know the last row containing data. This is derived from the total number of records you are exporting.
- Use
AfterSheetHook: Register a listener for theAfterSheetevent within your Export class. - Apply Style Dynamically: Inside the listener, calculate the range dynamically using PHP's array manipulation capabilities and pass this calculated range to the styling method.
Here is how you can implement this dynamic alignment:
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use Illuminate\Support\Collection;
class InvoiceExport implements FromView, WithEvents
{
public function view(): \Illuminate\Contracts\View\View
{
$data = \App\Models\Invoice::all(); // Example: Get all invoices
return view('exports.item', [
'data' => $data
]);
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$sheet = $event->sheet;
// 1. Determine the last row index based on the data count
// Note: If your data starts at row 2 (header is row 1), adjust accordingly.
$lastRow = $sheet->getHighestRow(); // Gets the highest populated row number dynamically
if ($lastRow > 1) {
// Define the range dynamically for Column C, starting from row 2 up to the last row
$range = 'C2:' . \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(3) . $lastRow;
// 2. Apply the alignment style to the determined dynamic range
$sheet->getStyle($range)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
}
},
];
}
}
Best Practices for Laravel Development
When building solutions within Laravel, especially when dealing with external libraries like those used in the Excel context (which often rely on underlying structures similar to how data is managed in Eloquent), it is crucial to favor dynamic execution over static assumptions. This principle aligns perfectly with the philosophy of robust software development championed by companies like Laravel Company. Focusing on runtime data and leveraging events ensures that your application remains flexible and resilient, regardless of changes in input size.
By using getHighestRow() within the AfterSheet event, we ensure that the formatting operation scales perfectly with the exported data set. This method avoids the pitfalls of hardcoding limits, making your export functionality reusable and scalable for any future growth in your application's data volume.
Conclusion
Setting text alignment to the right across a dynamic column in a Laravel Excel export requires moving beyond static range definitions. By mastering the use of event listeners like AfterSheet and dynamically calculating the range using methods provided by PhpSpreadsheet, you gain complete control over the output. This approach ensures that your exports are not only visually correct but also robust and scalable for any data size.