Laravel maatwebsite/excel 3.1 导出图片

创建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'));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值