Home > PHP Framework > Laravel > A brief analysis of the basic usage of Laravel-excel3.1

A brief analysis of the basic usage of Laravel-excel3.1

藏色散人
Release: 2021-12-28 15:40:54
forward
1469 people have browsed it

The following tutorial column of Laravel will introduce you to the most basic usage of Laravel-excel3.1. I hope it will be helpful to you!

  • Official website: https://docs.laravel-excel.com

The route is defined as GET

Route::get('/export', [OrderController::class, 'export']);
Copy after login

Controller forwarding

public function export(Request $request): BinaryFileResponse{
     return Excel::download(new OrderExport($request->get('status', -1)), 'order.xlsx');}
Copy after login

Business code

<?php

namespace App\Exports;

use App\Models\Order;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

/**
 * 派单导出
 */
class OrderExport implements FromCollection, WithHeadings, WithColumnFormatting, WithMapping
{
    public $status;

    //接受参数
    public function __construct(int $status)
    {
        $this->status = $status;
    }

    /**
     * 数据源
     * @return Collection
     */
    public function collection(): Collection
    {
        $query = Order::query();
        if ($this->status != -1) {
            $query->where(&#39;status&#39;, $this->status);
        }
        return $query->get();
    }

    /**
     * 自定义表头
     * @return string[]
     */
    public function headings(): array
    {
        return [
            &#39;编号&#39;,
            &#39;创建人&#39;,
            &#39;中队长&#39;,
            &#39;人员&#39;,
            &#39;名称&#39;,
            &#39;备注&#39;,
            &#39;状态&#39;,
            &#39;创建时间&#39;,
            &#39;更新时间&#39;
        ];
    }

    /**
     * 设置单元格时间格式
     * @return array
     */
    public function columnFormats(): array
    {
        return [
            &#39;H&#39; => NumberFormat::FORMAT_DATE_YYYYMMDD,
            &#39;I&#39; => NumberFormat::FORMAT_DATE_YYYYMMDD,
        ];
    }

    /**
     * 自定义数据列
     * @param mixed $row
     * @return array
     */
    public function map($row): array
    {
        return [
            $row->id,
            $row->founder->name ?? &#39;无&#39;,
            $row->squadron->name ?? &#39;无&#39;,
            $row->player->name ?? &#39;无&#39;,
            $row->name,
            $row->remark ?: &#39;无&#39;,
            $this->statusMap($row->status),
            Date::dateTimeToExcel($row->created_at),
            Date::dateTimeToExcel($row->updated_at),
        ];
    }

    /**
     * 状态转化
     * @param $status
     * @return string
     */
    public function statusMap($status): string
    {
        switch ($status) {
            case 0:
                $statusText = &#39;待处理&#39;;
                break;
            case 1:
                $statusText = &#39;处理中&#39;;
                break;
            case 2:
                $statusText = &#39;待审核&#39;;
                break;
            case 3:
                $statusText = &#39;已完成&#39;;
                break;
            default:
                $statusText = &#39;未知&#39;;
        }
        return $statusText;
    }
}
Copy after login

Related recommendations: The latest five Laravel video tutorials                                                                                    

The above is the detailed content of A brief analysis of the basic usage of Laravel-excel3.1. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template