【发布时间】:2023-01-16 11:08:53
【问题描述】:
我在这里使用这个参考 > https://docs.laravel-excel.com/3.1/exports/collection.html
我使用Maatwebsite 的 3.1 版来映射数据并通过 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);
}
它产生这个输出:
问题:如何添加一个动态数据,垂直汇总Sales、Income、Both。
我想要这样的期望输出:
所以基本上是一个自定义数据,它汇总了摘要并在每个数组结束后动态添加。我的 Total 函数数组分配给单元格 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 的情况下修改代码?截至目前,它将从一个单元格跳到另一个单元格
【问题讨论】:
-
你有没有解决这个问题。文档真的很难分开和解决。
-
是的@warmwhisky,但使用硬代码。我会发布一个答案
标签: laravel laravel-5.8 maatwebsite-excel laravel-excel