如何更改 Excel Laravel 中的列名称?

How to change columns names in Excel Laravel?

我使用 this library 将集合中的数据导出到 excel 文件。默认情况下,我将列名称设置为 table 字段的名称。

如何自行替换列名?

我有自己的摘要class:

abstract class Excel
{ 
    
    abstract public function export();

    public function download()
    {
        $this->file = \Excel::create($this->filename, function ($excel) {
            $excel->sheet($this->sheetname, function ($sheet) {
                $sheet->fromArray($this->data->toArray());
            });
        })->store($this->typefile, $this->path_save);
    }

}

我的 excel 文件:

<?php

    namespace App\Library;
    
    use App\Library\Excel;
    use App\DistributorContacts;
    use App\PersonalityTraits;
    use App\Helpers\Helper;
    use Maatwebsite\Excel\Concerns\WithHeadings;
    
    
    class ExcelConclusions extends Excel implements WithHeadings
    {
    
    
        public $type = "_conclusions";
    
    
        public function headings(): array
        {
            return ["your", "headings", "here"];
        }
    
        public function export()
        {}
    }

在您的导出 class 中使用这样的界面标题:

namespace App\Exports;

use App\NameOfTable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class NameOfExport implements FromCollection , WithHeadings{

    public function collection()
    {
        return NameOfTable::get();
    }

     public function headings():array
        {
            return [
                'NameOfFirstColumn',
                'NameOfSecondColumn,
        ....
        
            ];
        } 
}

您应该定义导出的所有列。

我有一个控制器 ExportExcel 调用定义为 Exports 的导出 class

<?php

namespace App\Http\Controllers;

use App\Exports\nameOfExpport;
use Maatwebsite\Excel\Facades\Excel;

    class ExportExcel extends Controller
    {
        //
        public function functionName() {
            return Excel::download(new nameOfExpport() , 'fileName');
        }

.....