[已解决]请教关于在Laravel-Admin中使用Laravel-Excel V3.* 导出带图片的EXCEL
- Laravel Version: ^7.0
- PHP Version:^7.3
- Laravel-admin: ^1.7
- Laravel-Excel:^3.1
<?php
namespace App\Admin\Extensions;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\Exportable;
class StocksExporter extends ExcelExporter implements WithDrawings,ShouldAutoSize,WithTitle,WithEvents,WithMapping
{
use Exportable;
protected $fileName = '采购订单.xlsx';
protected $columns = [
'website_id' => '店铺',
'start_at' => '订单日期',
'product_title' => '产品标题',
'img_url' => '图片',
'product_option' => '产品选项',
'required_count' => '采购数量',
];
public function __construct()
{
$this->fileName = '采购订单'.request()->start_at.'.xlsx';
}
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$event->sheet->getColumnDimension('C')->setAutoSize(false)->setWidth(10);
$event->sheet->getColumnDimension('E')->setAutoSize(false)->setWidth(15);
}
];
}
public function title(): string
{
return request()->start_at;
}
public function map($stock) : array
{
return [
data_get($stock, 'website.url'),
$stock->start_at->format('Y-m-d'),
$stock->product_title.' - '.$stock->product_option,
$stock->img_url,
$stock->product_option,
$stock->required_count,
];
}
public function drawings()
{
$i = 1;
$result = [];
foreach($this->getCollection() as $val) {
$i++;
$url = $val['img_url'];
$file_name = basename(parse_url($url, PHP_URL_PATH));
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
// $bool = Storage::disk('admin')->put($file_name, file_get_contents($url, false, stream_context_create($arrContextOptions)));
${'drawing'.$i} = new Drawing();
${'drawing'.$i}->setName('1111');
${'drawing'.$i}->setPath(public_path('uploads/'.$file_name));
${'drawing'.$i}->setHeight(50);
${'drawing'.$i}->setCoordinates('D'.$i);
$result[] = ${'drawing'.$i};
}
return $result;
}
}
问题描述:
导出的结果中,图片插入和数据不是同一行,如下图
推荐文章: