phplaravelxlsmaatwebsite-excellaravel-excel

Laravel - Laravel Excel - maatwebsite Successfully added image to spreadsheets, the image is 'over cell', how to make it 'in cell'?


Here is my Export Class, function drawings () :

public function drawings()
    {
        $drawingCollection = [];        
        $row = 2;
        $column = 'G';

        foreach ($this->reportData as $data) {
            $drawing = new Drawing();
            $drawing->setName('MyImage');
            $drawing->setDescription('Description');
            if($data->{'Foto Toko Depan'})
            {
                $imagePath = storage_path('app/public/' . $data->{'Foto Toko Depan'});
                $drawing->setPath($imagePath);                
                $drawing->setHeight(20);
                $drawing->setWidth(20);
                $drawing->setCoordinates($column . $row);
                $drawingCollection[] = $drawing;
            }
            $row++;
        }

        return $drawingCollection;
   }

After downloaded, it looks like this: Spreadsheet Screenshot

It looks a mess, because it is 'over' cell, not 'in cell' , is there any way to fix this?

Setting row height was not really useful, because the image is 'over cell'


Solution

  • You can't achieve that. it works like an overlay. However, you can set the cell height to match the image height, creating a similar effect.

    public function registerEvents(): array
        {
          return [
                     AfterSheet::class => function(AfterSheet $event) {
                         for ($i =1; $i <= $this->count; $i++) {
                             $event->sheet->getDelegate()->getRowDimension($i)->setRowHeight(100);
                         }
                     }
                ];
        }
    

    You can pass the number of rows from your controller like this

    public function export()
        {
            $count = User::count();
            $export = new UsersExport($count);
            return Excel::download($export, 'users.xlsx');
        }