Hamid Naghipour
Hamid Naghipour

Reputation: 3635

laravel maatwebsite-excel change sheet direction

how can I change sheet direction in laravel maatwebsite excel package? I want to change from left to right.

A B C D

to:

D C B A

this is my code :

   $row =  $this->userRepository->getById(1);

        $data = $row;
        Excel::create('test', function ($excel) use($data) {

            $excel->sheet('sheet', function ($sheet) use($data) {
                $sheet->fromArray($data, null, 'A1', false, false);
                $sheet->row(1, function($row) {
                });

            });
        })->store('xlsx', storage_path('excel/exports'))->download('xlsx');

Upvotes: 4

Views: 2530

Answers (4)

M. Sameer Hussain
M. Sameer Hussain

Reputation: 11

First Add :

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\{
  BeforeExport,
  AfterSheet, 
 };
class UploadResult implements  WithStyles, ShouldAutoSize, WithEvents { // add here
    /**
    * @return \Illuminate\Support\Collection
    */

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function(AfterSheet $event) {
                $event->sheet->getDelegate()->setRightToLeft(true); // this change
            },
        ];
    }
}

To change the Excel sheet direction you can use setRightToLeft(true) as in the below example

Link : documentation maatwebsite

Upvotes: 1

Hossein Esteki
Hossein Esteki

Reputation: 71

class TractsExport implements FromCollection,WithEvents
{

    public function registerEvents(): array
    {
        return [

            BeforeSheet::class  =>function(BeforeSheet $event){
                $event->getDelegate()->setRightToLeft(true);
            }
        ];
    }
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        retutn Tract::all();
}

Upvotes: 7

jamal
jamal

Reputation: 183

as laravel-Excel is made based on phpExcel then you can use native phpExcel method to the $excel object or the $sheet object

here is the code to change the sheet direction

 $excel->sheet('sheet', function ($sheet) use($data) {
                $sheet->->setRightToLeft(true);
                $sheet->fromArray($data, null, 'A1', false, false);
                $sheet->row(1, function($row) {
                });

Upvotes: 1

Mohsen mokhtari
Mohsen mokhtari

Reputation: 3062

you can use simply a foreach in your row and use array_reverse($row) before submitting to excel

Upvotes: 2

Related Questions