代码之家  ›  专栏  ›  技术社区  ›  JUSTIN CHUA

导出Laravel excel图像失败

  •  0
  • JUSTIN CHUA  · 技术社区  · 1 年前

    正在尝试通过laravel excel导出我的daanswerdata。 https://docs.laravel-excel.com/3.1/getting-started/- 这是我用来参考的文档。起初,它运行成功,但当我打开导出的文件时,它会导出图像的路径,而不是导出实际的图像。是否有方法导出图像(png文件)?

    这是导出的文件 exported file

    这是保存图像的本地路径文件夹 local path

    在我的数据库中是这样的 db structure .

    这是我的代码:

    1.出口。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、出口控制器

       public function exportexcel()
        {
    
            return Excel::download(new SignatureExport, 'signaturelist.xlsx');
    
        }
    
    

    我错过什么了吗?感谢您的回答。


    用被接受的答案解决了问题。 但是出现了新的问题

    仅成功导出了1个图像,而不是3个图像 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,          
        ];
    }
    
    
    }
    
    

    我应该怎么做才能导出所有图像?

    1 回复  |  直到 1 年前
        1
  •  1
  •   Anuj Shrestha    1 年前

    在SignatureExport类文件中,缺少 WithDrawings 机具

    class SignatureExport implements WithDrawings //like this
    

    所以你的 SignatureExport 类应该是这样的

    class SignatureExport implements FromCollection, WithHeadings, WithColumnWidths, WithDrawings
    

    use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

    此导入仅用于实例 $drawing = new Drawing(); 对象 要使导出文件正常工作,您需要添加 withDrawing 您正在添加的担忧 WithHeadings 以及其他