Search code examples
laravelvue.jsaxiosexport-to-excel

Download xlsx file without Headers, Vuejs


I have a download action, that it works, but in my .xlsx file I don't have the header, and I am not sure how can I get them. I'm using Vuejs / Axios with Laravel.

<a type="button" class="mr-3" href="/json/persons/export" download="file.xlsx">
<button @click="exportCSV" class="btn btn-primary">
    Export CSV
</button>
</a>

exportCSV() {
    axios
    .get("/json/persons/export", {
        params: {
        sort_by: this.sortBy,
        sort_direction: this.sortDesc
        }
    })
    .then(response => {
        //
    })
    .catch(error => {
        //
    });
},

Export Class Code

namespace App\Exports;
use App\ViewData;
use Maatwebsite\Excel\Concerns\FromCollection;
use Excel;

class DataExport implements FromCollection
{
    public function collection()
    {
        return ViewData::all();
    }

}

Solution

  • Well you can actually do something like

    namespace App\Exports;
    use App\ViewData;
    use Maatwebsite\Excel\Concerns\FromCollection;
    use Maatwebsite\Excel\Concerns\WithHeadings;
    use Excel;
    
    class DataExport implements FromCollection, WithHeadings
    {
        public function collection()
        {
            return ViewData::all();
        }
    
        //Define your desired headings this way, it's just for an example
        public function headings(): array
        {
           return [
               'Name',
               'Surname',
               'Email',
               'Twitter',
           ];
        }
    }