首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel Excel 3.0/3.1导出文件

问Laravel Excel 3.0/3.1导出文件
EN

Stack Overflow用户
提问于 2019-01-28 15:14:30
回答 1查看 999关注 0票数 0

以下是我的代码

代码语言:javascript
复制
class ReportController extends Controller{
    return (new Report)->download('report.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}

class Report implements FromQuery {
    use Exportable;
    public function query(){
        return User::query();
    }    
}

将excel导出到我的计算机是一项工作,但是如果我想从每一行添加或检查一些值,我不知道如何才能做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2019-03-07 13:31:52

你所需要做的就是:

创建一个类ExportReport.php

代码语言:javascript
复制
<?php

namespace NeonMobile\Exports;


class ExportReport implements WithMultipleSheets
{
    use Exportable; 

    public function __construct($data = [])
    {
        $this->myRepo = \App::make(ReportRepositoryInterface::class);
    }

    /**
     * @return array
     */
    public function sheets(): array
    {
        $reportData= $this->myRepo->myFunctionToGetData();
        $sheets[] = new ExportMultipleSheets([
            'collection' => collect($reportData),
            'title'      => 'export_invoice',
            'cells'      => ['A1:G1'],
        ]);

        return $sheets;
    }
}

和另一个公共类ExportMultipleSheets.php

代码语言:javascript
复制
<?php

namespace NeonMobile\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;

class ExportMultipleSheets implements FromCollection, WithTitle, ShouldAutoSize, WithEvents
{
    private $title;
    private $collection;
    private $cells;

    public function __construct($data = [])
    {
        $this->title = $data['title'];
        $this->collection = $data['collection'];
        $this->cells = $data['cells'];
    }

    /**
     * @return Collection
     */
    public function collection()
    {
        return $this->collection;
    }

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

现在调用just

代码语言:javascript
复制
class ReportController extends Controller{
    return (new ReportExport)->download('report.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54397123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档