Insert image with Laravel-Excel
Asked Answered
B

2

8

I'm using this package to generate excel documents with Laravel: https://github.com/Maatwebsite/Laravel-Excel

However, documentation doesn't say anything about inserting images from files into my excel document. I'm pretty sure it's possible with native phpexcel itself, but how to do this through this package?

Some example code would be much appreciated..

Berwick answered 19/7, 2015 at 11:25 Comment(1)
I think you have to create a blade view with a table in it and images in the table cells and then use that to generate the excel document. If you have a way to do it in php please let me know.Land
M
13

first you need declare Class use PHPExcel_Worksheet_Drawing;

and in controller :

$filename = 'File Name';
    
Excel::create($filename, function($excel){    
    $excel->sheet('sheet name', function($sheet){
        $objDrawing = new PHPExcel_Worksheet_Drawing;
        $objDrawing->setPath(public_path('img/headerKop.png')); //your image path
        $objDrawing->setCoordinates('A2');
        $objDrawing->setWorksheet($sheet);
    });
})->export('xls');
Marvelous answered 4/1, 2016 at 13:53 Comment(2)
@Marvelous if we use new PHPExcel_Worksheet_Drawing, its says PHPExcel_Worksheet_Drawing' not found should we need to include any libraries for it.Caswell
@Crysis, please include >> use PHPExcel_Worksheet_Drawing; at the top of your pageSouvaine
D
3

For new version 3.1

Import

use Maatwebsite\Excel\Concerns\WithDrawings;

use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

and

class InvoicesExport implements WithDrawings

public function drawings()
{
    $drawing = new Drawing();
    $drawing->setName('Logo');
    $drawing->setDescription('This is my logo');
    $drawing->setPath(public_path('/img/vir.png'));
    $drawing->setHeight(90);
    $drawing->setCoordinates('B3');

    return $drawing;
}
Dermatophyte answered 18/3, 2020 at 23:7 Comment(2)
is there a way that, image can be fetched from a url (someimgae.jpg) instead of "public_path('/img/vir.png')"Papacy
Thanks bro. It worked!Dmso

© 2022 - 2024 — McMap. All rights reserved.