如何在导出 table 时创建新列。 Laravel 网站 / excel
How to create a new column when exporting a table. Laravel maatwebsite / excel
请告诉我,如何在导出(到 exel)时创建新列 table。这种类型的数据库中有一个table:
我安装了导出包maatwebsite / excel.
还有一个我的模型文件:
class ScheduledInspectionModel extends Model
{
protected $table = 'scheduled_inspection'; // table name
protected $fillable = ['name_smp', 'name_control', "verification_start", "verification_end", 'verification_duration'];
public $timestamps = false;
}
控制器:
class OrganizationsExportController extends Controller
{
public function export()
{
return (new OrganizationsExport)->download('organizations_export.xls');
}
}
以及带有导出说明的文件:
class OrganizationsExport implements FromCollection, ShouldAutoSize, WithHeadings, WithEvents
{
use Exportable;
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return ScheduledInspectionModel::all();
}
public function headings(): array
{
return [
'id',
'Name SMP',
'Name Control',
'Verification Start',
'Verification End',
'Verification Duration'
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getStyle('A1:F1')->applyFromArray([
'font' => [
'bold' => true
]
]);
}
];
}
}
导出的 table 如下所示:
导出有效 :) 但我想创建 'id' 列(我可以使用 map () 排除它)、'Number' 列并相应地输入行号。请告诉我该怎么做?
我会在导出时使用 map()
函数,在这里您可以调整每列的来源。由于不知道结构,我假定了您数据库中的列名。每次转换时将计数加一,您应该是金色的,这是通过 ++
运算符完成的。
private $count = 0;
public function map(ScheduledInspectionModel $inspection): array
{
return [
++$this->count,
$inspection->name_smp,
$inspection->name_control,
$inspection->verification_start->format('Y-m-d') . ' - ' .$inspection->verification_end->format('Y-m-d'),
$inspection->duration,
];
}
要调用日期格式,您必须在模型中设置日期数组。
class ScheduledInspectionModel {
protected $dates = [
'verification_start',
'verification_end',
];
}
请告诉我,如何在导出(到 exel)时创建新列 table。这种类型的数据库中有一个table:
我安装了导出包maatwebsite / excel.
还有一个我的模型文件:
class ScheduledInspectionModel extends Model
{
protected $table = 'scheduled_inspection'; // table name
protected $fillable = ['name_smp', 'name_control', "verification_start", "verification_end", 'verification_duration'];
public $timestamps = false;
}
控制器:
class OrganizationsExportController extends Controller
{
public function export()
{
return (new OrganizationsExport)->download('organizations_export.xls');
}
}
以及带有导出说明的文件:
class OrganizationsExport implements FromCollection, ShouldAutoSize, WithHeadings, WithEvents
{
use Exportable;
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return ScheduledInspectionModel::all();
}
public function headings(): array
{
return [
'id',
'Name SMP',
'Name Control',
'Verification Start',
'Verification End',
'Verification Duration'
];
}
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$event->sheet->getStyle('A1:F1')->applyFromArray([
'font' => [
'bold' => true
]
]);
}
];
}
}
导出的 table 如下所示:
导出有效 :) 但我想创建 'id' 列(我可以使用 map () 排除它)、'Number' 列并相应地输入行号。请告诉我该怎么做?
我会在导出时使用 map()
函数,在这里您可以调整每列的来源。由于不知道结构,我假定了您数据库中的列名。每次转换时将计数加一,您应该是金色的,这是通过 ++
运算符完成的。
private $count = 0;
public function map(ScheduledInspectionModel $inspection): array
{
return [
++$this->count,
$inspection->name_smp,
$inspection->name_control,
$inspection->verification_start->format('Y-m-d') . ' - ' .$inspection->verification_end->format('Y-m-d'),
$inspection->duration,
];
}
要调用日期格式,您必须在模型中设置日期数组。
class ScheduledInspectionModel {
protected $dates = [
'verification_start',
'verification_end',
];
}