如何解决Maatwebsite / laravel 为导出中的工作表命名
我正在使用 Maatwebsite / laravel 导出数据,我需要重命名工作表,
在我的班级以下:
<?php
namespace App\Exports;
use App\Models\Client;
use App\Models\Item;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
class ItemExport implements FromCollection,WithHeadings,ShouldAutoSize
{
protected $rows;
use Exportable;
public function __construct($client_id,$items,$company_name)
{
$this->client_id = $client_id;
$this->items = $items;
$this->company_name = $company_name;
}
public function collection()
{
$i = 1;
$client = Client::find($this->client_id);
$item_new= [];
foreach($this->items as $item){
$item_new[] = [$i++,$this->company_name,$this->item,$client->client_name,];
}
return collect($item_new);
}
public function title(): string
{
return $this->company_name;
}
public function headings(): array
{
return [
'#','Company','item','client',];
}
}
但似乎公共函数 title(): string , not working 。 我认为标题功能会起作用,但似乎没有 知道如何给工作表命名吗?
谢谢
解决方法
这么近。您只需要实现 WithTitle
接口。
class ItemExport implements FromCollection,WithHeadings,WithTitle,ShouldAutoSize
文档的多页部分中隐藏了一个示例,但我可以确认它也适用于单页。
https://docs.laravel-excel.com/3.1/exports/multiple-sheets.html#sheet-classes
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。