在 laravel excel 中的集合末尾放置一列
Place a column at the end of a collection in laravel excel
我有一个 excel 文件,其中包含获取社区的查询以及每个社区的总记录。但是当查询的所有数据都放在 excel 自定义列中时,我怎么能把它放在最后呢?这是我的代码
class NeighborhoodExport implements FromCollection,WithHeadings, ShouldAutoSize, WithEvents,WithTitle
{
/* *
* @return \Illuminate\Support\Collection
*/
protected $start;
protected $end;
function __construct($start,$end) {
$this->start = $start;
$this->end = $end;
}
/**
* @return \Illuminate\Support\Collection
*/
public function title(): string
{
return 'Neighborhood';
}
public function collection()
{
return DB::table('users')->select("neighborhood"),DB::raw("COUNT(neighborhoods) as count"))
->whereBetween('created_at', [$this->start, $this->end])
->groupBy('neighborhood')->get();
}
public function headings(): array
{
return [
'Neighborhood',
'Count',
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:B1'; // All headers
$styleArray = [
'font' => [
'name' => 'Calibri',
'size' => 11,
'bold' => false,
'color' => ['argb' => 'FFFFFF'],
],
//Set background style
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => '000000',
]
],
];
$event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
},
];
}
}
excel中有一个列总数的例子。就这样
我在文档中找到了函数 registerEvents 上的追加事件。这就是我的代码现在的样子
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:B1'; // All headers
$styleArray = [
'font' => [
'name' => 'Calibri',
'size' => 11,
'bold' => false,
'color' => ['argb' => 'FFFFFF'],
],
//Set background style
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => '000000',
]
],
];
$total = User::select('neighborhood')->whereBetween('created_at', [$this->start, $this->end])->count();
$event->sheet->appendRows(array(
array('Total', $total),
//....
), $event);
$event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
},
];
https://docs.laravel-excel.com/2.1/export/rows.html#append-row
我有一个 excel 文件,其中包含获取社区的查询以及每个社区的总记录。但是当查询的所有数据都放在 excel 自定义列中时,我怎么能把它放在最后呢?这是我的代码
class NeighborhoodExport implements FromCollection,WithHeadings, ShouldAutoSize, WithEvents,WithTitle
{
/* *
* @return \Illuminate\Support\Collection
*/
protected $start;
protected $end;
function __construct($start,$end) {
$this->start = $start;
$this->end = $end;
}
/**
* @return \Illuminate\Support\Collection
*/
public function title(): string
{
return 'Neighborhood';
}
public function collection()
{
return DB::table('users')->select("neighborhood"),DB::raw("COUNT(neighborhoods) as count"))
->whereBetween('created_at', [$this->start, $this->end])
->groupBy('neighborhood')->get();
}
public function headings(): array
{
return [
'Neighborhood',
'Count',
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:B1'; // All headers
$styleArray = [
'font' => [
'name' => 'Calibri',
'size' => 11,
'bold' => false,
'color' => ['argb' => 'FFFFFF'],
],
//Set background style
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => '000000',
]
],
];
$event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
},
];
}
}
excel中有一个列总数的例子。就这样
我在文档中找到了函数 registerEvents 上的追加事件。这就是我的代码现在的样子
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:B1'; // All headers
$styleArray = [
'font' => [
'name' => 'Calibri',
'size' => 11,
'bold' => false,
'color' => ['argb' => 'FFFFFF'],
],
//Set background style
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'startColor' => [
'rgb' => '000000',
]
],
];
$total = User::select('neighborhood')->whereBetween('created_at', [$this->start, $this->end])->count();
$event->sheet->appendRows(array(
array('Total', $total),
//....
), $event);
$event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
},
];
https://docs.laravel-excel.com/2.1/export/rows.html#append-row