maatwebsite/excel使用:导出——插图 | laravel china 社区-金年会app官方网

通过使用 withdrawings ,你可以向工作表中添加一个或多个插图。

创建插图实例

首先,你需要实例化一个新的 \phpoffice\phpspreadsheet\worksheet\drawing,并为其属性分配有意义的值。

$drawing = new \phpoffice\phpspreadsheet\worksheet\drawing();
$drawing->setname('logo');
$drawing->setdescription('这是我的标志');
$drawing->setpath(public_path('/img/logo.jpg'));
$drawing->setheight(90);

你可以在 中查看 drawing 的所有可用属性。

添加单个插图

当你实例化了插图后,可以在导出类中添加 withdrawings ,并在 drawings 方法中返回插图实例。


namespace app\exports;
use maatwebsite\excel\concerns\withdrawings;
use phpoffice\phpspreadsheet\worksheet\drawing;
class invoicesexport implements withdrawings
{
    public function drawings()
    {
        $drawing = new drawing();
        $drawing->setname('logo');
        $drawing->setdescription('这是我的标志');
        $drawing->setpath(public_path('/img/logo.jpg'));
        $drawing->setheight(90);
        $drawing->setcoordinates('b3');
        return $drawing;
    }
}

添加多个插图

你可以通过在 drawings 方法中返回一个数组来向工作表中添加多个插图。


namespace app\exports;
use maatwebsite\excel\concerns\withdrawings;
use phpoffice\phpspreadsheet\worksheet\drawing;
class invoicesexport implements withdrawings
{
    public function drawings()
    {
        $drawing = new drawing();
        $drawing->setname('logo');
        $drawing->setdescription('这是我的标志');
        $drawing->setpath(public_path('/img/logo.jpg'));
        $drawing->setheight(50);
        $drawing->setcoordinates('b3');
        $drawing2 = new drawing();
        $drawing2->setname('其他图片');
        $drawing2->setdescription('这是第二张图片');
        $drawing2->setpath(public_path('/img/other.jpg'));
        $drawing2->setheight(120);
        $drawing2->setcoordinates('g2');
        return [$drawing, $drawing2];
    }
}

添加远程图片的插图

你可以通过实例化一个新的 \phpoffice\phpspreadsheet\worksheet\memorydrawing,并从外部 url 获取二进制图像数据,然后将其赋值给 setimageresource。在 drawings 方法中返回插图实例。


namespace app\exports;
use maatwebsite\excel\concerns\withdrawings;
use phpoffice\phpspreadsheet\worksheet\memorydrawing;
class invoicesexport implements withdrawings
{
    public function drawings()
    {
        if (!$imageresource = @imagecreatefromstring(file_get_contents('http://example.jpg'))) {
            throw new \exception('无法将图片 url 转换为图像资源。');
        }
        $drawing = new memorydrawing();
        $drawing->setname('logo');
        $drawing->setdescription('这是我的标志');
        $drawing->setimageresource($imageresource);
        $drawing->setheight(90);
        $drawing->setcoordinates('b3');
        return $drawing;
    }
}
本作品采用《cc 协议》,转载必须注明作者和本文链接
从零开发一个电商项目,功能包括电商后台、商品 & sku 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
网站地图