使用 Laravel-Excel 插入图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31500689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:58:14  来源:igfitidea点击:

Insert image with Laravel-Excel

laravellaravel-5phpexcellaravel-excel

提问by Boring person

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

我正在使用这个包用 Laravel 生成 excel 文档: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?

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

Some example code would be much appreciated..

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

回答by randawahyup

first you need declare Class use PHPExcel_Worksheet_Drawing;

首先你需要声明类 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');

this is working on me, hope this help you :)

这对我有用,希望这对你有帮助:)

回答by Samuel Cruz

For new version 3.1

对于新版本 3.1

Import

进口

use Maatwebsite\Excel\Concerns\WithDrawings;

使用 Maatwebsite\Excel\Concerns\WithDrawings;

use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

使用 PhpOffice\PhpSpreadsheet\Worksheet\Drawing;

and

class InvoicesExport implements WithDrawings

类 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;
}