laravel-admin 导出Excel多个sheet

作者:谢高升 发布:2020-11-18 浏览:3627次

laravel-admin 导出多个sheet的excel,扩展使用的是 

maatwebsite/excel": "^3.1"
//控制器中调用
$grid->exporter(new PeopleExporter());

//下面 是PeopleExporter 代码

namespace App\Admin\Extensions;

use App\Models\People;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use App\Exports\Sheets\PeopleSheet;

class PeopleExporter extends ExcelExporter implements WithMultipleSheets
{
    protected $fileName ='1.xlsx';

    use Exportable;
    public function __construct(Grid $grid = null)
    {
        $type = request('type',1);
        $name = People::$typeArr[$type];
        $this->fileName = $name.'_'.date('Ymd').'.xlsx';
    }

    public function sheets(): array
    {
        $type = request('type',1);
        $sheets = [];
        $export = request('_export_');
        $where = [];
        $where['type'] = $type;
       
        $people = People::where($where);
        $peoples =  $people->get();
            if ($peoples){
                foreach ($peoples as $peo){
                    $sheets[] = new PeopleSheet(
                    $peo->id,$peo->name,$type,$success_at);
                }
            }
        return $sheets;
    }

}

//下面是PeopleSheet的主要代码

namespace App\Exports\Sheets;

use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class PeopleSheet  implements FromQuery, WithTitle, WithHeadings, WithMapping
{
    private $pid;
    private $name;
    private $type;
    private $success_at;

    public function __construct(int $pid,$name,$type=1,$success_at='')
    {
        $this->pid = $pid;
        $this->name = $name;
        $this->type = $type;
        $this->success_at = $success_at;
    }

    /**
     * @return Builder
     */
    public function query()
    {
         return ReportForms::query()
                ->select('col1','col2','col3','col4','col5')
                ->where('pid',$this->pid);

    }

    /**
     * @return string
     */
    public function title(): string
    {
        return $this->name;
    }

    public function headings(): array
    {
       return [
                '日期',
                '职场名称',
                '姓名',
                '业务名称',
                '成功号码',
            ];

    }

    /**
     * @var ReportForms $invoice
     */
    public function map($invoice): array
    {
        return [
                $invoice->col1,
                $this->getColName($invoice->col2,1),
                $this->getColName($invoice->col3,2),
                $invoice->col4,
                $invoice->col5,
            ];

    }

    public function getColName($col,$num)
    {
        switch ($num){
            case 1:
                $data = Service::find($col);
                return $data?$data->name:'';
            case 2:
                $data = Pingtai::find($col);
                return $data?$data->name:'';
          
        }
    }
}