Home > front end >  Export Laravel excel Image failed
Export Laravel excel Image failed

Time:06-26

Dotrying to export my daanswerdata through laravel excel. exported file

this is the local path folder where the image saved local path

this is how its look like in my database db structure .

here is my code:

1.Export.php

<?php

namespace App\Exports;

use App\Models\signature;
use Maatwebsite\Excel\Concerns\FromCollection;

//frow laravel excel  drawing(image export) 
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Maatwebsite\Excel\Concerns\WithHeadings;

use Maatwebsite\Excel\Concerns\WithColumnWidths;





class SignatureExport implements FromCollection,  WithHeadings, WithColumnWidths
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return signature::all();
    }


    public function drawings()
    {
    

        $drawing = new Drawing();

    
        $drawing->setName('signature');
        $drawing->setDescription('This is my signatuer');
        $drawing->setPath(public_path('/uploads/signatures'));
        
        $drawing->setHeight(90);
        $drawing->setCoordinates('D1');

        return $drawing;
    }

    public function headings(): array
    {
        return [
            'id',
            'name',
            'description',
            'signature',
            'created_at',
            'updated_at',
        ];
    }

    public function columnWidths(): array
{
    return [
        'A' => 30,
        'B' => 30,        
        'C' => 30,
        'D' => 30,    
        'E' => 30,          
    ];
}


}

2.Export Controller

   public function exportexcel()
    {

        return Excel::download(new SignatureExport, 'signaturelist.xlsx');

    }

Do I miss anything? any answer is appreciated.


problem solved with the answers that are being accepted . but ther is new problem came out

instead of 3 image , only 1 image exported sucessfully only 1


<?php

namespace App\Exports;

use App\Models\signature;
use Maatwebsite\Excel\Concerns\FromCollection;

//frow laravel excel  drawing(image export) 
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

use Maatwebsite\Excel\Concerns\WithDrawings;

use Maatwebsite\Excel\Concerns\WithHeadings;

use Maatwebsite\Excel\Concerns\WithColumnWidths;

use Maatwebsite\Excel\Concerns\WithColumnFormatting;






class SignatureExport implements FromCollection,  WithHeadings, WithColumnWidths, WithDrawings
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        return signature::all();
    }


    public function drawings()
    {
    

        $drawing = new Drawing();

        $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
        $drawing->setName('signature');
        $drawing->setDescription('This is my signatuer');
        $drawing->setPath(public_path('/uploads/signatures/62ac8a575af1a.png'));
        
        $drawing->setHeight(90);
        $drawing->setCoordinates('D1');

        return $drawing;
    }

    public function headings(): array
    {
        return [
            'id',
            'name',
            'description',
            'signature',
            'created_at',
            'updated_at',
        ];
    }

    public function columnWidths(): array
{
    return [
        'A' => 30,
        'B' => 30,        
        'C' => 30,
        'D' => 30,    
        'E' => 30,          
    ];
}


}

what should i do to get all image being exported?

CodePudding user response:

In your SignatureExport class file you are missing the WithDrawings implements

class SignatureExport implements WithDrawings //like this

So your SignatureExport class should look something like this

class SignatureExport implements FromCollection, WithHeadings, WithColumnWidths, WithDrawings

use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

This import is just to instance $drawing = new Drawing(); object. For the export file to work you will need to add withDrawing concerns like you are adding WithHeadings and others

  • Related