Laravel Maatwebsite Excel Text Align Center Example
In this example, i will show you laravel maatwebsite excel set text align center. this example will help you laravel excel horizontal align center. We will look at example of laravel excel vertical align center. let’s discuss about laravel PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER excel file. follow bellow step for laravel maatwebsite excel set text align center.
We will use setHorizontal() for set text align center in laravel maatwesite composer package. you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
You can follow bellow url for checking full example of Import and Export Excel File with laravel. Here i will give you solution with export class:
Laravel Import Export Excel and CSV File Tutorial
Solution:
app/Exports/UsersExport.php
<?php
namespace App\Exports;
use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
class UsersExport implements FromCollection, WithHeadings, WithEvents
{
/**
* Write code on Method
*
* @return response()
*/
public function collection()
{
return User::select("id", "name", "email")->get();
}
/**
* Write code on Method
*
* @return response()
*/
public function headings() :array
{
return [
'ID',
'Name',
'Email',
];
}
/**
* Write code on Method
*
* @return response()
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->getDelegate()->getStyle('A1:C1')
->getAlignment()
->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
},
];
}
}
Output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Maatwebsite Excel Set Font Color Example
- Laravel Maatwebsite Excel Set Cell Height and Width Example
- Laravel Maatwebsite Excel Increase Font Size Example
- Laravel Maatwebsite Excel Fix Header Row using freezePane()
- Laravel Maatwebsite Excel Set Background Color Example
- Laravel - Create CSV File from Custom Array using Maatwebsite
- How to Validate Excel Sheet Data in Laravel?
- Laravel 8 Import Export Excel and CSV File Tutorial
- Laravel Yajra Datatables Export to Excel CSV Button Example
- Laravel Collection Except() Method Example
- How to add header row in export excel file with maatwebsite in Laravel 5.8?