8

我正在使用这个包通过 Laravel 生成 excel 文档: https ://github.com/Maatwebsite/Laravel-Excel

但是,文档没有说明将文件中的图像插入到我的 excel 文档中。我很确定原生 phpexcel 本身是可能的,但是如何通过这个包做到这一点?

一些示例代码将不胜感激..

4

2 回答 2

13

首先你需要声明类use PHPExcel_Worksheet_Drawing;

在控制器中:

$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');
于 2016-01-04T13:53:30.627 回答
3

对于新版本 3.1

进口

使用 Maatwebsite\Excel\Concerns\WithDrawings;

使用 PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

类 InvoicesExport 实现 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;
}
于 2020-03-18T23:07:41.487 回答