Laravel Excel Export - Formatting and Styling Cells - Laravel Daily
Laravel Excel Export - Formatting and Styling Cells - Laravel Daily
Laravel-Excel package is great for exporting data. But not a lot of info there about
formatting Excel cells – widths, word wraps, fonts etc. So when I encountered this
in a client’s project, I decided to write this article with a few tips on this topic.
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
https://laraveldaily.com/laravel-excel-export-formatting-and-styling-cells/ 1/5
3/8/2019 Laravel Excel Export: Formatting and Styling Cells - Laravel Daily
namespace App\Http\Controllers;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
function export()
{
return Excel::download(new UsersExport, 'users.xlsx');
}
It does the job, exports the data. But does it look good and readable? Far from it.
So let’s tweak it a little.
// ...
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
// ...
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
// ...
/**
* @return array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:W1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->getFont
},
];
}
https://laraveldaily.com/laravel-excel-export-formatting-and-styling-cells/ 3/5
3/8/2019 Laravel Excel Export: Formatting and Styling Cells - Laravel Daily
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
It just takes whatever cell range we pass, and changes the styles.
Here’s where we need to dig deeper and look at the package which is the base of
Laravel Excel. It’s actually based on PHPSpreadsheet package. So let’s take a look
at its documentation, and specifically section Recipes.
Wrap text:
$spreadsheet->getActiveSheet()->getStyle('A1:D4')
->getAlignment()->setWrapText(true);
$spreadsheet->getDefaultStyle()->getFont()->setName('Arial');
$spreadsheet->getDefaultStyle()->getFont()->setSize(8);
$styleArray = [
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER
'color' => ['argb' => 'FFFF0000'],
],
],
];
$worksheet->getStyle('B2:G8')->applyFromArray($styleArray);
You can find more examples in that recipes section, just apply it to your individual
needs.
That’s it, that’s how you format exported excel with Laravel and PHP.
Related articles:
https://laraveldaily.com/laravel-excel-export-formatting-and-styling-cells/ 4/5
3/8/2019 Laravel Excel Export: Formatting and Styling Cells - Laravel Daily
Povilas Korop
PHP web-developer with 15 years experience, 5 years with Laravel. Now leading a small team of developers,
growing Laravel adminpanel generator QuickAdminPanel and publishing Laravel courses on Teachable.
https://laraveldaily.com/laravel-excel-export-formatting-and-styling-cells/ 5/5