Export image to excel laravel

Laravel Excel version 3.1

1. Data preparation

Build two tables, add some data, check the data in the controller, and use it for the template.

Table 1-order: id, order_no, img_path, note

Table 2-order_item: id, order_id, sku, num

Check data:

$data = OrderModel::with('item')->get()->toArray(); //使用关联模型hasmany查询 $data = $data[0]; dd($data);

Print result:

View Image

2. Template preparation

New template:

resources/views/order_export.blade.php

<table> <thead> </thead> <tbody> <tr> <td colspan="2" style="vertical-align: center;"><b>订单号</b></td> <td colspan="2" style="vertical-align: center;"><b>备注</b></td> <td colspan="2" style="vertical-align: center;"><b>图片</b></td> </tr> <tr> <td colspan="2" style="vertical-align: center;">xxx</td> <td colspan="2" style="vertical-align: center;">xxx</td> <td colspan="2" style="vertical-align: center;">xxx</td> </tr> <tr> <td colspan="2" style="vertical-align: center;"><b>SKU</b></td> <td colspan="2" style="vertical-align: center;"><b>数量</b></td> </tr> <tr> <td colspan="2" style="vertical-align: center;">xxx</td> <td colspan="2" style="vertical-align: center;">xxx</td> </tr> </tbody> </table>

3. New export class

php artisan make:export OrderExport //app/Exports/OrderExport.php

OrderExport

3.1. Implement the FromView interface

Implement the view() method

use Maatwebsite\Excel\Concerns\FromView; class OrderExport implements FromView { public function view(): View { $data = OrderModel::with('item')->get()->toArray();//见上1、数据准备 return view('order_export', ['data'=>$data[0]]); } }

3.2, adjust the template

<table> <thead> </thead> <tbody> <tr> <td colspan="2" style="vertical-align: center;"><b>订单号</b></td> <td colspan="2" style="vertical-align: center;"><b>备注</b></td> <td colspan="2" style="vertical-align: center;"><b>图片</b></td> </tr> <tr> <td colspan="2" style="vertical-align: center;">{{ $data['order_no'] }}</td> <td colspan="2" style="vertical-align: center;">{{ $data['note'] }}</td> <td colspan="2" style="vertical-align: center;">xxx</td> </tr> <tr> <td colspan="2" style="vertical-align: center;"><b>SKU</b></td> <td colspan="2" style="vertical-align: center;"><b>数量</b></td> </tr> @foreach($data['item'] as $k=>$item) <tr> <td colspan="2" style="vertical-align: center;">{{ $item['sku'] }}</td> <td colspan="2" style="vertical-align: center;">{{ $item['num'] }}</td> </tr> @endforeach </tbody> </table>

3.3, call in the controller

//控制器中写个方法 public function index() { return Excel::download(new OrderExport(), 'test.xlsx'); } //路由指向,即可导出

3.4. Export results:

View Image

At this time, the export has been completed, but there are some things that need to be noted. The css in the template can only be embedded and only a few styles can be used. The specific ones have not been tried, only knowing the border line will not work. In addition, the image img tag is not applicable, and the effect will not be output when exporting.

4. Picture export

Image export needs to use WithEvents to monitor events, which is not mentioned in this manual.

Or the above derived class: OrderExport, need to implement WithEvents and implement the registerEvents() method

When the excel sheet is monitored, insert a picture:

use Maatwebsite\Excel\Concerns\FromView; use Maatwebsite\Excel\Concerns\WithEvents; class OrderExport implements FromView,WithEvents { public function view(): View { $data = OrderModel::with('item')->get()->toArray(); return view('order_export', ['data'=>$data[0]]); } public function registerEvents(): array { return [ AfterSheet::class => function(AfterSheet $event){ $data = OrderModel::find(1); $this->setImage2Excel($event, 'E2' ,$data['img_path'], 0,90); //E2时表格横E竖2,图片需要出现的位置 }]; } /** * 添加图片到excel * @param $event * @param $position:excel表位置 * @param $path:图片路径 * @param $width:图片宽度 * @param $height:图片高度 * @throws/PhpOffice\PhpSpreadsheet\Exception */ private function setImage2Excel($event, $position, $path, $width,$height){ $drawing = new\PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); $drawing->setName('Logo'); $drawing->setDescription('Logo'); $drawing->setCoordinates($position); $drawing->setPath(public_path($path)); ($width==0)?null:$drawing->setWidth($width); ($height==0)?null:$drawing->setHeight($height); $drawing->setWorksheet($event->sheet->getDelegate()); } }

The setImage2Excel() method is encapsulated by itself and is convenient to call when multiple images are needed to export.

result:

View Image