Auto size Excel column width

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Excel Exports: How to Auto-Size Columns in Laravel Excel

As developers working with data migration and reporting tools, exporting data into formats like Excel requires more than just writing rows of data; it requires managing the presentation layer as well. When using powerful packages like the popular Laravel Excel (Maatwebsite/Laravel-Excel), achieving perfect column auto-sizing can sometimes be tricky because you are dealing with two different systems: your application logic and the specific rendering rules of Microsoft Excel.

I’ve seen this exact frustration many times—you populate your sheet perfectly, but when it hits Excel, the columns remain stubbornly narrow or too wide. This post will dive into why the simple command $sheet->setAutoSize('true'); often fails and provide you with a robust, developer-centric solution to ensure your Excel exports are perfectly formatted.

Why Direct Auto-Sizing Fails in Laravel Excel

You correctly identified that attempting to use $sheet->setAutoSize('true'); did not yield the desired result for auto-sizing column widths. This happens because the Laravel Excel package primarily focuses on mapping PHP arrays directly into spreadsheet cells. While it handles data insertion flawlessly, the method for invoking the actual autofit feature within the underlying library often requires specific context or post-processing that isn't automatically triggered by a simple flag.

Excel’s autofit mechanism relies on calculating the maximum width required to display the longest string of text within that column. If the data you provide doesn't force Excel to recognize the necessary boundaries consistently, the command may be ignored or misapplied. This is a common hurdle when bridging application logic with proprietary file formats.

The Developer Solution: Calculating Widths Programmatically

Instead of relying on a potentially unreliable internal flag, the most robust solution is to calculate the required width for each column before writing it to the sheet and apply that calculated value manually. This approach puts full control back into your hands, ensuring precision regardless of the export engine's quirks.

The core concept is simple: iterate through your data, find the maximum length of content in each column, and set the column width accordingly.

Here is a practical example demonstrating how you can achieve true auto-sizing by calculating widths based on cell content:

use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;

class DataExporter
{
    public function exportData(array $data)
    {
        // 1. Prepare data and determine maximum string lengths for each column
        $headers = array_keys($data[0] ?? []);
        $columnWidths = [];

        foreach ($headers as $index => $header) {
            $maxWidth = 0;
            foreach ($data as $row) {
                // Calculate the length of the current cell content
                $cellLength = strlen((string) $row[$index]);
                if ($cellLength > $maxWidth) {
                    $maxWidth = $cellLength;
                }
            }
            // Add a small buffer (e.g., +2) for readability
            $columnWidths[$index] = $maxWidth + 2;
        }

        // 2. Use the Excel facade to write the data with pre-calculated widths
        Excel::store(new \WithStyles($data, $columnWidths), 'report.xlsx');

        return true;
    }
}

Explanation of the Best Practice

In the example above, we bypass the direct reliance on $sheet->setAutoSize('true'). Instead, we perform a pre-calculation step:

  1. Identify Headers: We first determine which columns exist based on the data structure.
  2. Iterate and Measure: We loop through every row and every column index. For each cell, we use strlen() to find the length of the content.
  3. Determine Maximum: We track the maximum string length found in that specific column across all rows.
  4. Apply Buffer: We add a small buffer (in this case, +2) to ensure there is enough space for padding around the text, making the resulting spreadsheet much more readable.

This method ensures that the column width you define is mathematically derived from the data itself, eliminating ambiguity and guaranteeing proper auto-sizing when Excel opens the file. This level of meticulous control over your output aligns perfectly with the principles of building robust systems, similar to how large projects in the Laravel ecosystem are managed by organizations like https://laravelcompany.com.

Conclusion

When faced with specific formatting challenges in data exports, developers must shift from relying on simple flags to implementing explicit, programmatic logic. By calculating the maximum required width based on the content of your data before writing it to the spreadsheet, you gain complete control over the output. This practice transforms an unreliable guesswork scenario into a deterministic, reproducible process, ensuring that your Excel reports are always perfectly formatted and professional.