laravel maatwebsite /excel 3.0 数据导出

3.0数据导出在Export concerns 方法里面实现对应接口既可以导出数据

但是collect 和 query 都需要map实现

/**
 * 导出excel
 * @params $head 表头
 * @params $data 数据
 * @params $filed 表头对应字段名
 * @params $name 文件名称
 */
public function exportExcel($head, $data, $name, $filed)
{
    $head = isset($head) ? $head : '';
    $data = isset($data) ? $data : '';
    $name = isset($name) ? $name : '';

    if (empty($data) || empty($head) || empty($name) || $filed) {
        return null;
    }

    return (new Excel\InvoicesExport($head, $data, $filed))->download($name);
}

/**
 * 头信息
 * @return array
 */
public function headings(): array
{
    return $this->head;
}

/**
 * 数据
 * @param mixed $data
 * @return array
 */
public function map($data): array
{
    foreach ($this->filed as $t) {
        if (stripos($t, '.')) {
            $d = explode('.', $t);
            if (isset($data[$d[0]][$d[1]])) {
                $c[] = $data[$d[0]][$d[1]];
            }
        } else {
            if (isset($data[$t])) {
                $c[] = $data[$t];
            }
        }

    }
    return $c;
}

/**
 * 收集数据返回
 * @return \Illuminate\Support\Collection
 */
public function collection()
{
    return collect($this->res);
}

猜你喜欢

转载自blog.csdn.net/weixin_40076986/article/details/79972036