我正在使用Maatwebsite的 Laravel excel 3.1导出一个excel 。我想用2个标题将其映射并在另一个标题下方显示
我正在使用WithHeadings,WithMapping,FromArray 我正在获取记录,只是需要更正的格式
<?php
namespace App\Exports;
use Modules\Program\Entities\Program;
use Modules\report\Entities\report;
use DB;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithMapping;
class reportPerDaySheet implements FromArray, WithTitle, WithHeadings, ShouldAutoSize, WithMapping
{
private $year;
private $day;
private $month;
public function __construct(int $year, int $month, int $day)
{
$this->year = $year;
$this->day = $day;
$this->month = $month;
}
public function array():array
{
$reportOut = DB::table('reports')->join('programs','programs.program_id','=','reports.program_id')->whereMonth('report_dateofreport', $this->month)->whereday('report_dateofreport', $this->day)->get()->toArray();
return $reportOut;
}
public function map($reportOut):array
{
return [
[
$reportOut->program_programname,
$reportOut->program_projectlead,
$reportOut->program_dse,
$reportOut->program_extserviceprovider,
$reportOut->program_programid,
$reportOut->program_datatype,
],
[
$reportOut->report_id,
$reportOut->report_date,
$reportOut->report_url,
$reportOut->report_username,
$reportOut->report_reportertype,
$reportOut->report_productname,
$reportOut->report_verbatim,
$reportOut->report_priority,
$reportOut->report_aeraised,
]
];
}
public function title(): string
{
return $this->day .' '. date("F", mktime(0,0,0,$this->month,1)) .' '. $this->year;
}
public function headings(): array
{
return[
[
'Program Name',
'Project Lead',
'DS&E Contact',
'Name of external service provider',
'Prepared By',
'External Service Provider Contact Information',
'Time Period Covered',
'Program ID',
'Date of Report',
'Data Type',
'Signature'
],
[
'Id',
'Date of report',
'Date',
'URL',
'Username',
'Reporter Type',
'Product Name',
'Verbatim',
'Priority',
'AE Raised',
'User',
'Date From',
'Date Till',
'Record Created At',
'Record Updated At'
]
];
}
}
答案 0 :(得分:0)
根据您的情况,建议在array()
方法中构建包含标头的数组。标题行将始终显示为前x行。 (取决于您返回的数组数)
答案 1 :(得分:0)
我遇到了类似的情况,我需要在某些行添加标题,
将function map()
视为foreach()
,它将遍历所有数据,这意味着您可以添加一些检查并将标题添加为另一行。
地图功能示例:
public function map($reportOut): array
{
if($this->addHeadingRow == 1) {
$this->addHeadingRow = 0; // for my use case I only want to add the heading once
$map = [
[], // add an empty array if you would like an spacer row
[
'heading 1',
'heading 2',
'and so on...'
]
];
}
else{
$map = [
$reportOut->program_programname,
$reportOut->program_projectlead,
$reportOut->program_dse,
$reportOut->program_extserviceprovider,
$reportOut->program_programid,
$reportOut->program_datatype,
];
}
return $map;
}