如何在laravel excel 3.1版中给数组列添加动态总和[页脚]?

krcsximq  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(157)

我在这里使用的引用是〉https://docs.laravel-excel.com/3.1/exports/collection.html
我使用3.1版的Maatwebsite来Map数据,并通过excel文件提取数据。
这是我用来导出数据的控制器函数。

public function exportTestData(Request $request)
{
    $timestamp = Carbon::now()->toDateTimeString();
    $filename = "act-data-({$timestamp}).xlsx";

    // getActData >> the function of the sql raw query
    return Excel::download(new TestDataExport($this->getActData($request, true)), $filename);
}

它会产生以下输出:

    • 问题:**如何添加一个动态数据,以垂直汇总SalesIncomeBoth的汇总。

我想要一个如下所示的输出:

所以基本上是一个自定义的数据,在每个数组结束后,动态的加总,我的Total函数array assign给单元格H2,水平的加总数据,如何在这种集成中,垂直的加总每个数据,试图找到stackoverflow,laracasts,和github的参考,但是我找不到任何好的资源,有人能帮我解决这个问题吗?谢谢。
下面是我的导出函数的完整代码,以了解编码过程。

    • 测试数据导出. php**
<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;

class TestDataExport implements FromCollection, ShouldAutoSize, WithCustomStartCell, WithHeadings, WithTitle, WithEvents
{
    use Exportable;

    /**
     * testDatas.
     *
     * @var string
     */
    protected $actData;

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

    public function startCell(): string
    {
        return 'A2';
    }

    public function registerEvents(): array
    {
        return [
            AfterSheet::class => function (AfterSheet $event) {
                /** @var Sheet $sheet */
                $sheet = $event->sheet;

                // map header data to this cells
                $sheet->mergeCells('E1:H1');
                $sheet->setCellValue('E1', "Summaries");

                $styleArray = [
                    'alignment' => [
                        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
                    ],
                ];

                $cellRange = 'A1:X1'; // All headers center align
                $event->sheet->getDelegate()->getStyle($cellRange)->applyFromArray($styleArray);
            },
        ];
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $testDatas = $this->testDatas;

        if (isset($testDatas)) {
            return collect($testDatas)->map(function ($actData, $key) {

                //define totals
                $totals = ! empty($actData['Summaries']) ? $actData['Summaries']['total'] : '0';

                return [
                    'sales' => ! empty($actData['Summaries']) ? $actData['Summaries']['sales'] : '0',
                    'income' => ! empty($actData['Summaries']) ? $actData['Summaries']['income'] : '0',
                    'both' => ! empty($actData['Summaries']) ? $actData['Summaries']['both'] : '0',
                    'total' => ! empty($totals) ? $totals : '0',
                ];
            });
        }

        return collect([]);
    }

    /**
     * Heading of the excel
     *
     * @return array
     */
    public function headings(): array
    {
        return [
            'Sales',
            'Income',
            'Both',
            'Total',
        ];
    }

    /**
     * Title for each sheet
     *
     * @return string
     */
    public function title(): string
    {
        return 'Test Data Export';
    }
}
    • 更新**

我在我的registerEvents函数中使用了这个函数。

//calculate totals
    
                $sheet->setCellValue('V'. ($sheet->getHighestRow()+1), '=SUM(V3:V'.$sheet->getHighestRow().')');
                $sheet->setCellValue('W'. ($sheet->getHighestRow()+1), '=SUM(W3:W'.$sheet->getHighestRow().')');
                $sheet->setCellValue('X'. ($sheet->getHighestRow()+1), '=SUM(X3:X'.$sheet->getHighestRow().')');
                $sheet->setCellValue('Y'. ($sheet->getHighestRow()+1), '=SUM(Y3:Y'.$sheet->getHighestRow().')');

它给出了每一行的合计值,但是我也遇到了一个问题,每次我给每一行添加一个新的合计值,合计值就会增加1。
此处输出:

有没有办法修改代码而不加1?到目前为止,它将从一个单元格跳到另一个单元格

ibps3vxo

ibps3vxo1#

使用styles函数解决了这个问题

public function styles(Worksheet $sheet)
    {
        $styleArray = [
        'font'  => [
            'bold'  => true,
            'color' => ['rgb' => 'FF0000'],
        ]];

        $sheet->setCellValue('D' . ($sheet->getHighestRow()+1), 'TOTALS')->getStyle('D' . ($sheet->getHighestRow()))->applyFromArray($styleArray);

        // Add cell with SUM formula to last row
        $sheet->setCellValue('E' . ($sheet->getHighestRow()+0), '=SUM(E2:E' . ($sheet->getHighestRow()-1) . ')')->getStyle('E' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('F' . ($sheet->getHighestRow()+0), '=SUM(F2:F' . ($sheet->getHighestRow()-1) . ')')->getStyle('F' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('G' . ($sheet->getHighestRow()+0), '=SUM(G2:G' . ($sheet->getHighestRow()-1) . ')')->getStyle('G' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('H' . ($sheet->getHighestRow()+0), '=SUM(H2:H' . ($sheet->getHighestRow()-1) . ')')->getStyle('H' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('I' . ($sheet->getHighestRow()+0), '=SUM(I2:I' . ($sheet->getHighestRow()-1) . ')')->getStyle('I' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('J' . ($sheet->getHighestRow()+0), '=SUM(J2:J' . ($sheet->getHighestRow()-1) . ')')->getStyle('J' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('K' . ($sheet->getHighestRow()+0), '=SUM(K2:K' . ($sheet->getHighestRow()-1) . ')')->getStyle('K' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('L' . ($sheet->getHighestRow()+0), '=SUM(L2:L' . ($sheet->getHighestRow()-1) . ')')->getStyle('L' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('M' . ($sheet->getHighestRow()+0), '=SUM(M2:M' . ($sheet->getHighestRow()-1) . ')')->getStyle('M' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('N' . ($sheet->getHighestRow()+0), '=SUM(N2:N' . ($sheet->getHighestRow()-1) . ')')->getStyle('N' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('O' . ($sheet->getHighestRow()+0), '=SUM(O2:O' . ($sheet->getHighestRow()-1) . ')')->getStyle('O' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('P' . ($sheet->getHighestRow()+0), '=SUM(P2:P' . ($sheet->getHighestRow()-1) . ')')->getStyle('P' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('Q' . ($sheet->getHighestRow()+0), '=SUM(Q2:Q' . ($sheet->getHighestRow()-1) . ')')->getStyle('Q' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('R' . ($sheet->getHighestRow()+0), '=SUM(R2:R' . ($sheet->getHighestRow()-1) . ')')->getStyle('R' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('S' . ($sheet->getHighestRow()+0), '=SUM(S2:S' . ($sheet->getHighestRow()-1) . ')')->getStyle('S' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('T' . ($sheet->getHighestRow()+0), '=SUM(T2:T' . ($sheet->getHighestRow()-1) . ')')->getStyle('T' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('U' . ($sheet->getHighestRow()+0), '=SUM(U2:U' . ($sheet->getHighestRow()-1) . ')')->getStyle('U' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('V' . ($sheet->getHighestRow()+0), '=SUM(V2:V' . ($sheet->getHighestRow()-1) . ')')->getStyle('V' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('W' . ($sheet->getHighestRow()+0), '=SUM(W2:W' . ($sheet->getHighestRow()-1) . ')')->getStyle('W' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('X' . ($sheet->getHighestRow()+0), '=SUM(X2:X' . ($sheet->getHighestRow()-1) . ')')->getStyle('X' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('Y' . ($sheet->getHighestRow()+0), '=SUM(Y2:Y' . ($sheet->getHighestRow()-1) . ')')->getStyle('Y' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('Z' . ($sheet->getHighestRow()+0), '=SUM(Z2:Z' . ($sheet->getHighestRow()-1) . ')')->getStyle('Z' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('AA' . ($sheet->getHighestRow()+0), '=SUM(AA2:AA' . ($sheet->getHighestRow()-1) . ')')->getStyle('AA' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('AB' . ($sheet->getHighestRow()+0), '=SUM(AB2:AB' . ($sheet->getHighestRow()-1) . ')')->getStyle('AB' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
        $sheet->setCellValue('AC' . ($sheet->getHighestRow()+0), '=SUM(AC2:AC' . ($sheet->getHighestRow()-1) . ')')->getStyle('AC' . ($sheet->getHighestRow()))->applyFromArray($styleArray);
    }

在第一个CellValue(D)中,此值将递增最后一个数据,并将其添加到正下方以计算总数。
对于下面的EAC,当我尝试对每个setCellValue使用$sheet->getHighestRow()+1)时,数据在每个单元格列后递增。我尝试过,但似乎当您在单元格EAC上添加$sheet->getHighestRow()+0)时,它将被放置在每个列的合计中。不知道为什么,也许你们中的一些人可以找到一个替代的或更好的方法来解决这个问题。

相关问题