-1

I use this library to export data from collection to excel file. By default I et columns names as namesd of table fields.

How to replace names of colums on own?

I have own abstract 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);
    }

}

My excel file:

<?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()
        {}
    }
  • this is a very well documented package, have you read through its documentation? – lagbox Dec 09 '21 at 14:23
  • There is not section about that ,I have tried –  Dec 09 '21 at 14:47
  • Does this answer your question? [How can you include column headers when exporting Eloquent to Excel in Laravel?](https://stackoverflow.com/questions/54245622/how-can-you-include-column-headers-when-exporting-eloquent-to-excel-in-laravel) – miken32 Dec 09 '21 at 16:17

1 Answers1

1

In your export class use the interface headings like that:

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,
        ....
        
            ];
        } 
}

You should define all the columns exported.

I have a controller ExportExcel that calls the exports defined as 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');
        }

.....
Radu
  • 995
  • 12
  • 23
  • Should I implement interface? –  Dec 09 '21 at 14:57
  • 1
    I have edited my answer, please see if it's working .... – Radu Dec 09 '21 at 15:00
  • 1
    If it helped, please select this as an answer (the checkmark bellow the answer) :) – Radu Dec 09 '21 at 15:09
  • Could you provide imports? –  Dec 09 '21 at 15:51
  • Checkout my question please –  Dec 09 '21 at 15:54
  • I use that to export a report in excel with my columns names this is what you asked .... – Radu Dec 09 '21 at 15:57
  • I have edited, hope it helps..... – Radu Dec 09 '21 at 16:05
  • Thank I try your way but always get this error: `Interface 'Maatwebsite\Excel\Concerns\WithHeadings' not found` –  Dec 09 '21 at 16:26
  • Use this: use Maatwebsite\Excel\Concerns\WithHeadings; in Exports class that you are using. – Radu Dec 09 '21 at 16:42
  • I have tried, no effect. I have updated question –  Dec 09 '21 at 16:44
  • 1
    I have updated the answer. – Radu Dec 09 '21 at 16:47
  • What is `namespace App\Exports;`? –  Dec 09 '21 at 16:56
  • I still get `FatalErrorException in ExcelConclusions.php line 12: Interface 'Maatwebsite\Excel\Concerns\WithHeadings' not found` –  Dec 09 '21 at 16:59
  • There are created all exports made by: php artisan make:export NameOfTheExport --model=User (first class in the answer) see also https://docs.laravel-excel.com/3.1/exports/ – Radu Dec 09 '21 at 17:00
  • I don know but this import not working –  Dec 09 '21 at 17:04
  • Try without using your abstract class and see if is working.... – Radu Dec 09 '21 at 17:05
  • Problem is I tried import `use Maatwebsite\Excel\Concerns\WithHeadings;` in HomeController. the same result –  Dec 09 '21 at 17:05
  • FatalErrorException in HomeController.php line 24: Interface 'Maatwebsite\Excel\Concerns\WithHeadings' not found –  Dec 09 '21 at 17:06
  • Maybe because you don't use namespace App\Exports; – Radu Dec 09 '21 at 17:07
  • Why if this file is placed by path `app\Library\Excel` and HomeController uses `namespace App\Http\Controllers;` –  Dec 09 '21 at 17:07
  • I see, that path (App\Exports) was created by Laravel Excel when installed. – Radu Dec 09 '21 at 17:10
  • So whats is namespace to use to import interface in home controller? –  Dec 09 '21 at 17:12
  • I have also namespace App\Http\Controllers; and use Maatwebsite\Excel\Facades\Excel; and use App\Exports\nameOfExport; in my controller – Radu Dec 09 '21 at 17:17
  • Interface 'Maatwebsite\Excel\Concerns\WithHeadings' not found –  Dec 09 '21 at 17:20
  • I have updated the answer for the controller that call the export. Again, namespace App\Exports is the default used by the package ... I think there is something wrong. – Radu Dec 09 '21 at 17:25
  • No effect, I dont know why –  Dec 09 '21 at 17:29
  • Did you create the export by using: php artisan make:export nameOfExport --model=User ? – Radu Dec 09 '21 at 17:33
  • `Undefined type 'Maatwebsite\Excel\Concerns\WithHeadings'.inteleph` –  Dec 09 '21 at 17:39
  • Could you connect to my PC? –  Dec 09 '21 at 17:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239987/discussion-between-alice-messis-and-radu). –  Dec 09 '21 at 17:42