如何在laravel excel版本3中添加标题和列的总和值? [英] How can I add title and sum value of column in laravel excel version 3?

查看:50
本文介绍了如何在laravel excel版本3中添加标题和列的总和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里获得参考: https://laravel-excel .maatwebsite.nl/docs/3.0/getting-started/basics

所以我使用版本3

我的控制器是这样的:

public function exportToExcel(Request $request)
{
    $data = $request->all();
    $exporter = app()->makeWith(SummaryExport::class, compact('data'));   
    return $exporter->download('Summary.xlsx');
}

我的脚本像这样导出到excel:

My script export to excel like this :

namespace App\Exports;
use App\Repositories\ItemRepository;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithHeadings;
class SummaryExport implements FromCollection, WithHeadings {
    use Exportable;
    protected $itemRepository;
    protected $data;
    public function __construct(ItemRepository $itemRepository, $data) {
        $this->itemRepository = $itemRepository;
        $this->data = $data;
    }
    public function collection()
    {
        $items = $this->itemRepository->getSummary($this->data);
        return $items;
    }
    public function headings(): array
    {
        return [
            'No',
            'Item Number',
            'Sold Quantity',
            'Profit'
        ];
    }
}

如果执行脚本,则结果如下:

If the script executed, the result like this :

我想在表格上方添加一些说明或标题,并希望对已售数量列和利润列求和

I want to add some description or title above the table and I want to sum sold quantity column and profit column

所以我想要这样的结果:

So I want the result like this :

我已经阅读了文档并在Google中搜索,但找不到解决方法

I had read the documentation and search in the google, but I don't find the solution

有没有人可以帮助您?

更新

从此参考文献中: https://laravel-excel.maatwebsite.nl /docs/3.0/export/extending

我尝试添加:

....
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeWriting;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\AfterSheet;
class SummaryExport implements FromCollection, WithHeadings, WithColumnFormatting, ShouldAutoSize, WithEvents
{
    ...
    public function registerEvents(): array
    {
        return [
            BeforeExport::class  => function(BeforeExport $event) {
                $event->writer->setCreator('Patrick');
            },
            AfterSheet::class    => function(AfterSheet $event) {
                $event->sheet->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);

                $event->sheet->styleCells(
                    'B2:G8',
                    [
                        'borders' => [
                            'outline' => [
                                'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                                'color' => ['argb' => 'FFFF0000'],
                            ],
                        ]
                    ]
                );
            },
        ];
    }
}

在上面的脚本中

但是存在这样的错误:

Method Maatwebsite\Excel\Sheet::styleCells does not exist
Method Maatwebsite\Excel\Sheet::setOrientation does not exist.
Method Maatwebsite\Excel\Writer::setCreator does not exist.

如何解决该错误?

推荐答案

您必须实例化一个这样的数组:-

You gotta instantiate an array like this:-

$export = [];

然后,您必须使用array_push将数据推入其中:-

Then you have to push your data in it using array_push:-

array_push($export, [
                    'No' => '',
                    'item'=>'',
                    'sold' =>'',
                    'profit' => ''
                ]);

然后您可以像这样追加您的计算:-

and then you can append your calculations like this:-

array_push($export,[' ','Items Count:', '=COUNTA(C2:C'.$Count.')' ,' ','Profit Sum:', '=SUM(D2:D'.$Count.')']);

如果添加任何标题,则$ count = count($ array + 1).

while the $count = count($array+1) if you add any headings.

您可以使用常规的单元格功能.

and you can use your normal cells functions.

这篇关于如何在laravel excel版本3中添加标题和列的总和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆