Laravel & Maatwebsite Excel: Set Excel Print Area and Margins and Print on One Page
This article explains the basic way to export data to Excel using Laravel and Maatwebsite\Excel with views, as well as how to configure the Excel print area, margins, and the “Fit Sheet on One Page” print setting.
Specifically, data is retrieved from a model, formatted through a view, and then exported to Excel.
Creating the Model
We will proceed assuming the use of the User model. Please adjust the method of retrieving data according to your project.
Creating the View
Create a view for exporting to Excel.
Here, let’s create resources/views/export/users.blade.php.
<!-- resources/views/export/users.blade.php -->
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
Creating the Export Class: Set Cell Print Area and Margins and Print on One Page
Create an export class that generates an Excel file using a view and a model.
namespace App\Exports;
use App\User;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class UsersExport implements FromView, WithEvents
{
public function view(): View
{
return view('export.users', [
'users' => User::all()
]);
}
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$sheet = $event->sheet->getDelegate();
// The following code sets the “Fit Sheet on One Page” print option.
$sheet->getPageSetup()
->setPrintArea('A1:E10'); // Set the print area from A1 to E10
->setFitToWidth(1) // Fit to one page width
->setFitToHeight(1); // Fit to one page height
$sheet->getPageMargins() // Set top, right, left, and bottom margins respectively
->setTop(0.75)
->setRight(0.7)
->setLeft(0.7)
->setBottom(0.75);
},
];
}
}
- setPrintArea(‘A1:E10’) – This sets the print area from A1 to E10.
- setFitToWidth(1) and setFitToHeight(1) are used to ensure that the sheet contents fit on a single page when printing.
Normally, the values specified for these methods represent how many pages the sheet spans in width or height. By setting both to 1, Excel’s print option is configured to “Fit Sheet on One Page.” - getPageMargins()->setTop(0.75)->setRight(0.7)->setLeft(0.7)->setBottom(0.75); – This sets the top, right, left, and bottom margins respectively (values are in inches).
Points to note
The above method is based on later versions of the PhpSpreadsheet library and Maatwebsite Excel version 3.x.
If you are using a different version, or if changes are made in future versions, please refer to the official documentation for details.
Creating the Controller
Next, create a controller that uses the export class to download the Excel file.
namespace App\Http\Controllers;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
class UsersController extends Controller
{
public function export()
{
return Excel::download(new UsersExport, 'users.xlsx');
}
}
Setting Up Routing
Finally, add a route to routes/web.php and specify the controller method.
use App\Http\Controllers\UsersController;
Route::get('users/export', [UsersController::class, 'export']);
With this setup, a request to /users/export triggers the export method of UsersController, outputs the Excel file through the export class, and downloads it. The print area and margins are configured within the export class.
*Please use this information at your own responsibility if you reuse it.*