Laravel Excel/PHP Excel: Import, modify, download

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel Excel: Mastering Import, Modification, and Download of Excel Files

As a senior developer working within the Laravel ecosystem, you frequently encounter the need to interact with external file formats. One of the most common tasks is using spreadsheets—specifically Excel—as a template for data export. When dealing with packages like maatwebsite/excel (Laravel Excel), developers often run into hurdles when trying to move beyond simple data dumping and start performing complex modifications on existing files.

I understand your frustration, LuMa. The initial attempt you made, while conceptually correct in wanting to link cell references to Eloquent models, hits a roadblock because the way Laravel Excel handles the loading process is designed primarily for exporting new data rather than deep structural editing of pre-existing templates.

This post will walk you through the correct, robust method for loading an existing template file, dynamically filling specific cells with data from your application (like the current user's name), and then downloading the modified result.


The Challenge: Export vs. Modification

The core confusion often lies in understanding the scope of the package. When you use methods like toCollection() or simple load(), you are instructing the package to generate an entirely new spreadsheet based on your Eloquent collections. This is straightforward and powerful for creating files.

However, modifying a file requires accessing the underlying structure—the worksheet objects themselves. You need to load the template as a raw spreadsheet object first, manipulate its cells, and then instruct the package to serialize that modified structure back into an Excel file format.

The Solution: Direct Worksheet Manipulation

To achieve dynamic cell filling, we must leverage the ability of Laravel Excel to work with the underlying PhpSpreadsheet library objects directly. This allows us to treat the loaded file not just as data, but as a mutable object.

Here is the step-by-step process to fill cell A2 with the current user's name from an Eloquent model:

Step 1: Load the Template as a Worksheet Object

Instead of using load(), we will use methods that allow us to access the specific worksheet object within the template file.

Step 2: Access and Set Cell Values

Once you have the worksheet object, you can use standard PhpSpreadsheet notation (which Laravel Excel supports) to set cell values directly. Note that Excel uses 1-based indexing for rows and columns, so 'A2' corresponds to row 2, column 1.

Code Example: Filling Dynamic Data

Let's assume you have a template named template.xlsx with a sheet named Sheet1. We want to put the current user's name into cell A2.

use Maatwebsite\Excel\Facades\Excel;
use App\Models\User; // Assuming you are using Eloquent models

class ExcelController extends Controller
{
    public function downloadModifiedTemplate(Request $request)
    {
        // 1. Get the data you want to inject
        $user = User::current(); // Or fetch the specific user you need

        // 2. Define the path to your template file
        $templateFile = storage_path('app/templates/template.xlsx');

        // 3. Load the workbook, specifying the sheet we want to modify
        Excel::load($templateFile, function ($sheet) use ($user) {
            // Access the worksheet object provided by the package
            
            // Target cell A2 (Row 2, Column 1)
            $sheet->setCellValue('A2', $user->name);

            // You can modify other cells dynamically here:
            // $sheet->setCellValue('B5', 'Export Date: ' . now()->toDateString());
        });

        // 4. Download the modified file
        return Excel::download($templateFile, 'modified_report.xlsx');
    }
}

Best Practices and Laravel Context

This approach demonstrates a powerful concept in application development: Separation of Concerns. You are separating the data retrieval (Eloquent) from the file manipulation (Laravel Excel), allowing your business logic to dictate exactly what gets written to the file structure.

When building complex data processing tools, relying on direct object manipulation, as shown above, gives you granular control that simple collection export methods do not offer. This level of control is crucial when creating tools that automate reporting or template generation within a larger Laravel application, making it an excellent fit for robust systems like those championed by the Laravel framework itself.

Conclusion

Stop searching for a direct Excel::cell('A2') method; instead, embrace the callback function provided by Excel::load(). By utilizing the worksheet object passed into that callback, you unlock the ability to perform true structural modifications on existing Excel files. This technique moves you from simply exporting data to actively creating dynamic, personalized reports, giving you complete control over the final output.