love_php/app/Exports/TestExport.php

98 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\FromCollection;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithDrawings;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use App\Exports\Sheets\TestSheet;
use App\Exports\Sheets\TestSheetII;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
class TestExport implements WithEvents,WithDrawings,FromArray
{
use Exportable;
protected $data;
protected $id;
public function __construct()
{
}
public function array(): array
{
return [
['供应商名称', '经营许可证'],
['测试供应商1', 'https://image.fulllinkai.com/202302/06/01728541-EE2A-4F20-A897-805050FF1748.png',],
['测试供应商2', ''],
];
}
//自定义单元格格式
public function columnFormats(): array
{ dd(2);
// return [
// 'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
// 'C' => NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE,
// ];
return [];
}
//自定义宽
public function columnWidths(): array
{ dd(3);
return [
'A'=>'20',
'B'=>'20',
'C'=>'20',
'D'=>'20',
'E'=>'20',
'F'=>'20',
'G'=>'20',
'H'=>'20',
'I'=>'20',
'J'=>'20',
'K'=>'20',
'L'=>'20',
'M'=>'20',
'N'=>'20',
];
}
//导出图片
public function drawings()
{
// $draw_arr = [1=>'img.png',2=>'img1.png'];
$draw_arr = ['https://image.fulllinkai.com/202302/06/01728541-EE2A-4F20-A897-805050FF1748.png', 'https://image.fulllinkai.com/202302/06/timg%20(2).jpg'];
$result = [];
foreach ($draw_arr as $k => $v) {
${'drawing'.$k} = new Drawing();
${'drawing'.$k}->setName('Other image');
${'drawing'.$k}->setDescription('This is a second image');
//图片路径
// ${'drawing'.$k}->setPath(public_path($v));
${'drawing'.$k}->setPath($v);
${'drawing'.$k}->setIsURL(true);
${'drawing'.$k}->setHeight(50);
//设置图片列
${'drawing'.$k}->setCoordinates('B'.$k);
$result[] = ${'drawing'.$k};
}
return $result;
}
//自定义高
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event){
$event->sheet->getDelegate()->getDefaultRowDimension()->setRowHeight(50);
},
];
}
}