创建UserExport
namespace App\Admin\Extensions\Exporter;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithDrawings;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
class UserExport implements FromArray, ShouldAutoSize, WithDrawings
{
protected $invoices;
protected $logo;
public function __construct(array $invoices, array $logo)
{
$this->invoices = $invoices;
$this->logo = $logo;
}
use Exportable;
public function array(): array
{
return $this->invoices;
}
public function drawings()
{
$result = [];
foreach ($this->logo as $k => $v) {
$k += 2;
${'drawing' . $k} = new Drawing();
${'drawing' . $k}->setName('头像');
${'drawing' . $k}->setDescription('头像');
//图片路径
${'drawing' . $k}->setPath(public_path($v));
${'drawing' . $k}->setHeight(50);
//设置图片列
${'drawing' . $k}->setCoordinates('C' . $k);
$result[] = ${'drawing' . $k};
}
return $result;
}
}
控制器
class SendController extends Controller
{
//
public function index()
{
return (Excel::download(new UserExport([
['用户名' ,'年龄' ,'头像'],
['张三', 15, ''],
['李四', 17, ''],
['王五', 18, ''],
['赵六', 19, ''],
], [
'1.jpg',
'2.jpg',
'3.jpg',
'4.jpg',
]), '用户列表.xlsx'));
}
}