如何在maatwebsite/excel中从2.x升级到3.x?
How to upgrade from 2.x to 3.x in maatwebsite/excel?
我将我的应用程序迁移到 laravel 5。7.When 在 composer.json 安装软件包 我从 "maatwebsite/excel:~2.1.0" 升级到 "maatwebsite/excel":“^3.1” .所以现在我的导出功能不起作用 anymore.I 试图跟随 https://docs.laravel-excel.com/3.1 中的升级但没有起作用 me.this 是用于旧版本的旧代码:
$claim = Claim::all();
$count = Claim::count();
$name = 'Liste réclamations '.date('d-m-Y H-i');
Excel::create($name, function($excel) use($claim, $count) {
$excel->sheet('Sheetname', function($sheet) use($claim, $count) {
$i = 2;
$rows = $count;
$rows++;
$sheet->setHeight(1, 25);
$sheet->setAutoSize(true);
$sheet->setBorder('A1:AH'.$rows, 'thin');
$sheet->row(1, array(
'Utilisateur',
'Référence',
'Infraction',
'Groupe oiseaux',
"Type de braconnage",
"Type de l'espèce",
"Nom de l'espèce",
"Autre info de l'espèce",
"Description de l'oiseau",
"Le nombre d'oiseau",
"Le nombre d'oiseaux mis en vente",
"Prix de vente",
"Lieu de la vente",
"Nombre d'oiseaux détenus",
"Interdiction de la chasse",
"Présence des forces de l'ordre",
"Contact des autorités compétentes",
'Liste des autorités compétentes',
"Intervention immédiate",
"Type d'intervention",
"L'objectif de la détention",
"Autre objectif",
"Indications sur l'état de l'oiseau",
"Autre indication",
"Lieu de l'infraction",
"Gouvernorat de l'infraction",
"Latitude",
"Longitude",
"Date de l'infraction",
"L'heure de l'infraction",
"Description de l'infraction",
"Etat",
"Date de création"
))->cells('A1:AH1', function($cells) {
$cells->setBackground('#1E86CF');
$cells->setFont(array(
'family' => 'Calibri',
'size' => '12',
'bold' => true
));
$cells->setFontColor('#ffffff');
});
foreach ($claim as $key => $claim) {
$braconnage = '';
$contact= '' ;
$braconnage_link = Claimtypelink::where('claim_id',$claim->id)->with('claimbrac')->get();
$contact_link = Claimcontactlink::where('claim_id',$claim->id)->with('claimcontactaut')->get();
if(isset($braconnage_link)){
foreach ($braconnage_link as $key => $value) {
$braconnage = $braconnage.$value->claimbrac['title_fr'].' | ';
}
}
if(isset($contact_link)){
foreach ($contact_link as $key => $value) {
$contact = $contact.$value->claimcontactaut['title_fr'].' | ';
}
}
$sheet->row($i, array(
$claim->user['name'],
$claim->reference,
$claim->name_infraction,
$claim->group_oiseau,
$braconnage,
$claim->type_espece,
$claim->bird['title_fr'],
$claim->type_espece_other,
$claim->description_oiseau,
$claim->num_espece,
$claim->num_espece_vente,
$claim->prix_vente,
$claim->lieu_vente,
$claim->num_espece_detenu,
$claim->interdiction_chasse,
$claim->presence_ordre,
$claim->contact_autorite,
$contact,
$claim->intervention_immediate,
$claim->type_intervention,
$claim->objectif_detention,
$claim->objectif_detention_other,
$claim->indication_etat_oiseau,
$claim->indication_etat_oiseau_other,
$claim->lieu_infraction,
$claim->governorate['title_fr'],
$claim->latitude,
$claim->longitude,
$claim->date_infraction,
$claim->time_infraction,
$claim->description_infraction,
$claim->etat,
$claim->created_at
));
$i++;
}
});
})->download('xls');
我已经重新创建了您的项目示例以完成这项工作:
完全按照我的步骤执行:
- 安装包:
composer require maatwebsite/excel
- 现在运行以下命令:
php artisan make:export ClaimsExport --model=Claim
- 这将创建 app/Exports/ClaimsExport。php。粘贴以下代码:
<?php
namespace App\Exports;
use App\Claim;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use Maatwebsite\Excel\Concerns\WithEvents;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\FromCollection;
class ClaimsExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents, WithMapping
{
//
public function headings(): array
{
return [
'Utilisateur',
'Référence',
'Infraction',
'Groupe oiseaux',
"Type de braconnage",
"Type de l'espèce",
"Nom de l'espèce",
"Autre info de l'espèce",
"Description de l'oiseau",
"Le nombre d'oiseau",
"Le nombre d'oiseaux mis en vente",
"Prix de vente",
"Lieu de la vente",
"Nombre d'oiseaux détenus",
"Interdiction de la chasse",
"Présence des forces de l'ordre",
"Contact des autorités compétentes",
'Liste des autorités compétentes',
"Intervention immédiate",
"Type d'intervention",
"L'objectif de la détention",
"Autre objectif",
"Indications sur l'état de l'oiseau",
"Autre indication",
"Lieu de l'infraction",
"Gouvernorat de l'infraction",
"Latitude",
"Longitude",
"Date de l'infraction",
"L'heure de l'infraction",
"Description de l'infraction",
"Etat",
"Date de création",
];
}
//
public function collection()
{
return Claim::with('user')->get();
}
//
public function map($claim): array
{
// TODO: following two lines are fake data for visualisation
$braconnage = 'A|B';
$contact = 'C|D';
// TODO: Implement this
// $braconnage_link = Claimtypelink::where('claim_id', $claim->id)->with('claimbrac')->get();
// $contact_link = Claimcontactlink::where('claim_id', $claim->id)->with('claimcontactaut')->get();
if (isset($braconnage_link)) {
foreach ($braconnage_link as $key => $value) {
$braconnage = $braconnage . $value->claimbrac['title_fr'] . ' | ';
}
}
if (isset($contact_link)) {
foreach ($contact_link as $key => $value) {
$contact = $contact . $value->claimcontactaut['title_fr'] . ' | ';
}
}
return [
$claim->user['name'],
$claim->reference,
$claim->name_infraction,
$claim->group_oiseau,
$braconnage,
$claim->type_espece,
$claim->bird['title_fr'],
$claim->type_espece_other,
$claim->description_oiseau,
$claim->num_espece,
$claim->num_espece_vente,
$claim->prix_vente,
$claim->lieu_vente,
$claim->num_espece_detenu,
$claim->interdiction_chasse,
$claim->presence_ordre,
$claim->contact_autorite,
$contact,
$claim->intervention_immediate,
$claim->type_intervention,
$claim->objectif_detention,
$claim->objectif_detention_other,
$claim->indication_etat_oiseau,
$claim->indication_etat_oiseau_other,
$claim->lieu_infraction,
$claim->governorate['title_fr'],
$claim->latitude,
$claim->longitude,
$claim->date_infraction,
$claim->time_infraction,
$claim->description_infraction,
$claim->etat,
$claim->created_at,
];
}
//
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$styleArray = [
'font' => [
'bold' => true,
'size' => 12,
'name' => 'Calibri',
'color' => ['argb' => 'FFFFFFFF'],
],
'borders' => [
'outline' => [
'borderStyle' => Border::BORDER_THIN,
],
],
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => [
'argb' => 'FF1E86CF',
],
],
];
$event->sheet->getDelegate()->getStyle('A1:AG1')->applyFromArray($styleArray);
},
];
}
}
你会在那里找到 TODO。尝试自己实施。我不知道 Claimtypelink 和 Claimcontactlink 有什么作用。
- 现在在控制器中添加以下方法:
public function export()
{
$name = 'Liste réclamations ' . date('d-m-Y H-i') . '.xlsx';
return Excel::download(new ClaimsExport, $name);
}
并将其添加到该文件的顶部:
use App\Exports\ClaimsExport;
use Maatwebsite\Excel\Facades\Excel;
- 内部路由添加:
Route::get('claims/export', 'ClaimController@export')->name('export');
一切顺利。
我将我的应用程序迁移到 laravel 5。7.When 在 composer.json 安装软件包 我从 "maatwebsite/excel:~2.1.0" 升级到 "maatwebsite/excel":“^3.1” .所以现在我的导出功能不起作用 anymore.I 试图跟随 https://docs.laravel-excel.com/3.1 中的升级但没有起作用 me.this 是用于旧版本的旧代码:
$claim = Claim::all();
$count = Claim::count();
$name = 'Liste réclamations '.date('d-m-Y H-i');
Excel::create($name, function($excel) use($claim, $count) {
$excel->sheet('Sheetname', function($sheet) use($claim, $count) {
$i = 2;
$rows = $count;
$rows++;
$sheet->setHeight(1, 25);
$sheet->setAutoSize(true);
$sheet->setBorder('A1:AH'.$rows, 'thin');
$sheet->row(1, array(
'Utilisateur',
'Référence',
'Infraction',
'Groupe oiseaux',
"Type de braconnage",
"Type de l'espèce",
"Nom de l'espèce",
"Autre info de l'espèce",
"Description de l'oiseau",
"Le nombre d'oiseau",
"Le nombre d'oiseaux mis en vente",
"Prix de vente",
"Lieu de la vente",
"Nombre d'oiseaux détenus",
"Interdiction de la chasse",
"Présence des forces de l'ordre",
"Contact des autorités compétentes",
'Liste des autorités compétentes',
"Intervention immédiate",
"Type d'intervention",
"L'objectif de la détention",
"Autre objectif",
"Indications sur l'état de l'oiseau",
"Autre indication",
"Lieu de l'infraction",
"Gouvernorat de l'infraction",
"Latitude",
"Longitude",
"Date de l'infraction",
"L'heure de l'infraction",
"Description de l'infraction",
"Etat",
"Date de création"
))->cells('A1:AH1', function($cells) {
$cells->setBackground('#1E86CF');
$cells->setFont(array(
'family' => 'Calibri',
'size' => '12',
'bold' => true
));
$cells->setFontColor('#ffffff');
});
foreach ($claim as $key => $claim) {
$braconnage = '';
$contact= '' ;
$braconnage_link = Claimtypelink::where('claim_id',$claim->id)->with('claimbrac')->get();
$contact_link = Claimcontactlink::where('claim_id',$claim->id)->with('claimcontactaut')->get();
if(isset($braconnage_link)){
foreach ($braconnage_link as $key => $value) {
$braconnage = $braconnage.$value->claimbrac['title_fr'].' | ';
}
}
if(isset($contact_link)){
foreach ($contact_link as $key => $value) {
$contact = $contact.$value->claimcontactaut['title_fr'].' | ';
}
}
$sheet->row($i, array(
$claim->user['name'],
$claim->reference,
$claim->name_infraction,
$claim->group_oiseau,
$braconnage,
$claim->type_espece,
$claim->bird['title_fr'],
$claim->type_espece_other,
$claim->description_oiseau,
$claim->num_espece,
$claim->num_espece_vente,
$claim->prix_vente,
$claim->lieu_vente,
$claim->num_espece_detenu,
$claim->interdiction_chasse,
$claim->presence_ordre,
$claim->contact_autorite,
$contact,
$claim->intervention_immediate,
$claim->type_intervention,
$claim->objectif_detention,
$claim->objectif_detention_other,
$claim->indication_etat_oiseau,
$claim->indication_etat_oiseau_other,
$claim->lieu_infraction,
$claim->governorate['title_fr'],
$claim->latitude,
$claim->longitude,
$claim->date_infraction,
$claim->time_infraction,
$claim->description_infraction,
$claim->etat,
$claim->created_at
));
$i++;
}
});
})->download('xls');
我已经重新创建了您的项目示例以完成这项工作: 完全按照我的步骤执行:
- 安装包:
composer require maatwebsite/excel
- 现在运行以下命令:
php artisan make:export ClaimsExport --model=Claim
- 这将创建 app/Exports/ClaimsExport。php。粘贴以下代码:
<?php
namespace App\Exports;
use App\Claim;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use Maatwebsite\Excel\Concerns\WithEvents;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\FromCollection;
class ClaimsExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents, WithMapping
{
//
public function headings(): array
{
return [
'Utilisateur',
'Référence',
'Infraction',
'Groupe oiseaux',
"Type de braconnage",
"Type de l'espèce",
"Nom de l'espèce",
"Autre info de l'espèce",
"Description de l'oiseau",
"Le nombre d'oiseau",
"Le nombre d'oiseaux mis en vente",
"Prix de vente",
"Lieu de la vente",
"Nombre d'oiseaux détenus",
"Interdiction de la chasse",
"Présence des forces de l'ordre",
"Contact des autorités compétentes",
'Liste des autorités compétentes',
"Intervention immédiate",
"Type d'intervention",
"L'objectif de la détention",
"Autre objectif",
"Indications sur l'état de l'oiseau",
"Autre indication",
"Lieu de l'infraction",
"Gouvernorat de l'infraction",
"Latitude",
"Longitude",
"Date de l'infraction",
"L'heure de l'infraction",
"Description de l'infraction",
"Etat",
"Date de création",
];
}
//
public function collection()
{
return Claim::with('user')->get();
}
//
public function map($claim): array
{
// TODO: following two lines are fake data for visualisation
$braconnage = 'A|B';
$contact = 'C|D';
// TODO: Implement this
// $braconnage_link = Claimtypelink::where('claim_id', $claim->id)->with('claimbrac')->get();
// $contact_link = Claimcontactlink::where('claim_id', $claim->id)->with('claimcontactaut')->get();
if (isset($braconnage_link)) {
foreach ($braconnage_link as $key => $value) {
$braconnage = $braconnage . $value->claimbrac['title_fr'] . ' | ';
}
}
if (isset($contact_link)) {
foreach ($contact_link as $key => $value) {
$contact = $contact . $value->claimcontactaut['title_fr'] . ' | ';
}
}
return [
$claim->user['name'],
$claim->reference,
$claim->name_infraction,
$claim->group_oiseau,
$braconnage,
$claim->type_espece,
$claim->bird['title_fr'],
$claim->type_espece_other,
$claim->description_oiseau,
$claim->num_espece,
$claim->num_espece_vente,
$claim->prix_vente,
$claim->lieu_vente,
$claim->num_espece_detenu,
$claim->interdiction_chasse,
$claim->presence_ordre,
$claim->contact_autorite,
$contact,
$claim->intervention_immediate,
$claim->type_intervention,
$claim->objectif_detention,
$claim->objectif_detention_other,
$claim->indication_etat_oiseau,
$claim->indication_etat_oiseau_other,
$claim->lieu_infraction,
$claim->governorate['title_fr'],
$claim->latitude,
$claim->longitude,
$claim->date_infraction,
$claim->time_infraction,
$claim->description_infraction,
$claim->etat,
$claim->created_at,
];
}
//
public function registerEvents(): array
{
return [
AfterSheet::class => function (AfterSheet $event) {
$styleArray = [
'font' => [
'bold' => true,
'size' => 12,
'name' => 'Calibri',
'color' => ['argb' => 'FFFFFFFF'],
],
'borders' => [
'outline' => [
'borderStyle' => Border::BORDER_THIN,
],
],
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => [
'argb' => 'FF1E86CF',
],
],
];
$event->sheet->getDelegate()->getStyle('A1:AG1')->applyFromArray($styleArray);
},
];
}
}
你会在那里找到 TODO。尝试自己实施。我不知道 Claimtypelink 和 Claimcontactlink 有什么作用。
- 现在在控制器中添加以下方法:
public function export()
{
$name = 'Liste réclamations ' . date('d-m-Y H-i') . '.xlsx';
return Excel::download(new ClaimsExport, $name);
}
并将其添加到该文件的顶部:
use App\Exports\ClaimsExport;
use Maatwebsite\Excel\Facades\Excel;
- 内部路由添加:
Route::get('claims/export', 'ClaimController@export')->name('export');
一切顺利。