laravel 导出数据加图片到excel

image

需求:将多行这样的数据,按一个优惠券属于多个店的,将用户领取情况导出到excel,如图的话三个店,六个用户就要导出18行数据

导出最后入下图

image

首先安装好laravel-excel:

composer require maatwebsite/excel:~2.1.0

php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"

然后在引入两个类

useEncore\Admin\Grid\Exporters\AbstractExporter;

useMaatwebsite\Excel\Facades\Excel;
还有

use PHPExcel_Worksheet_Drawing;

然后自定义导出类

image

主要是export函数


public function export()

{

    Excel::create('优惠券名单',function ($excel) {

    $excel->sheet('优惠券名单',function ($sheet) {

    $sheet->setStyle([

            'font' => [

            'name' => 'Calibri',

            'size' => 15,

          'bold' => false,

      ]

  ]);

$head= $this->head;

 //init列
    $title_array = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
                    'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH'];
    $rows = collect([$head]);
    $sheet->rows($rows);
    $arr = [];
    $autoNum=0;

 foreach ($this->getData()as $item)

 {

           $coupons_user= CouponsUser::where('coupons_id',$item['coupons_id'])->get();

           if (!$coupons_user->isEmpty())

          {

                   foreach ($coupons_user as $val)
                        {
                            $seller = explode(',',$item['seller_id']);

                            foreach ($seller as $v)
                            {
                                $seller_name = Seller::where('seller_id',$v)->first();
                                $user  = User::where('user_id',$val['user_id'])->first();
                                $arr[$autoNum]['seller_name'] = $seller_name['seller_name'];
                                $arr[$autoNum]['coupons_name'] = $item['coupons_name'];
                                $arr[$autoNum]['user_nickname'] = $user['user_nickname'];
                                $arr[$autoNum]['user_mobile'] = $user['user_mobile'];
                                $arr[$autoNum]['receive_time'] = date('Y:m:d H:i:s',$val['receive_time']);
                                $autoNum++;
//                                商家','优惠券','领取用户名称','领取用户电话','领取时间','用户头像'
                            }
                        }

             }

}

$sheet->rows($arr);//插入excel

//img img必须要独立处理

 foreach ($arras $k=> &$item)

{

        $user= User::where('user_mobile',$item['user_mobile'])->first();

        if(!empty($user['user_img'])&& file_exists(base_path(). '/public/uploads/' . $user['user_img'])){

                 $item['user_img']= base_path(). '/public/uploads/' . $user['user_img'];

                  $obj= new PHPExcel_Worksheet_Drawing();//使用phpExcel

                  $obj->setPath($item['user_img']);

                  $sp= $title_array[0 + 3];

                  $obj->setCoordinates('F'.($k+2));//设置图片要插入的单元格

                  $sheet->setHeight($k+ 2,65);//设置高度

                  $sheet->setWidth(array($sp=> 11));//设置宽度

                  $obj->setHeight(80);

                  $obj->setOffsetX(1);设置偏移量

                  $obj->setRotation(1);

                  $obj->setWorksheet($sheet);

        }

}

});

})->export('xls');

}

你可能感兴趣的:(laravel 导出数据加图片到excel)