以下是我的代码
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导出到我的计算机是一项工作,但是如果我想从每一行添加或检查一些值,我不知道如何才能做到这一点?
发布于 2019-03-07 13:31:52
你所需要做的就是:
创建一个类ExportReport.php
<?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
<?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
class ReportController extends Controller{
return (new ReportExport)->download('report.xlsx', \Maatwebsite\Excel\Excel::XLSX);
}https://stackoverflow.com/questions/54397123
复制相似问题