maatwebsite/Excel 3.1怎么分块导出?

我现在想把数据通过chunk分块丢到队列里 然后根据块的大小在队列对excel进行一行行进行写入,有例子吗?

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 5

3.1不能这么用,我也找了好久,问了同事才知道它自带一个query函数,模型::query 就是分块查询了,在官方文档的From Query位置

2年前 评论

<?php

namespace App\Exports;

use App\Models\User;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class UsersExport implements FromQuery,WithHeadings,WithMapping
{

public function __construct($where){
    $this->where = $where;
}

//laravel Excel的分块处理
public function query()
{
    $where = $this->where;
    return User::query()->where($where);
}

//标题
public function headings(): array
{
    $header = [
        'name'=>'姓名',
        'job'=>'职务',
        'craft'=>'工种',
        'politics_status'=>'政治面貌',
        'phone'=>'联系电话',
    ];

    return $header;

}

//处理数据
public function map($row): array
{
    $data = [
        'name'=>$row->name,
        'job'=>$row->job,
        'craft'=>$row->craft,
        'politics_status'=>$row->politics_status,
        'phone'=>$row->phone,
    ];
    return $data;
}

}

贴上实例,控制器里面调用还是一样的 return Excel::download(new UsersExport($data), ‘invoices.xlsx’);

2年前 评论
Toml 2年前

通过查询导出这种,如果查询出来的块数据,需要继续处理(比如再返回一个需要计算的属性:某某数量),该怎么写 :neutral_face:

9个月前 评论

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