dhruvin prajapati
dhruvin prajapati

Reputation: 321

Excel Export using laravel with vuejs

I am Doting Excel export using Laravel with Vuejs, somehow the Code is returning true value but can not Download Excel file, if I do normal request it will download the file, but in axios request, it will not export the file

I am using php artisan make:export to export file

In App/Export/studentexport.php

public function collection()
{
    return Student_master::all();
}

then in controller i will do a function

public function export()
{
    return Excel::download(new StudentExport, 'users.xlsx');
}

In my Vue file i will write a code that cal call the controller and export the file

axios.get('api/export')
            .then(()=>{
                toast({
                    type: 'success',
                    title: 'Export the Data'
                })
            })
            .catch(()=> {
                toast({
                        type: 'warning',
                        title: 'Can not Export'
                        })
            })

but the result is like that

that will return True, I really don't know how to solve this,plase help me

Upvotes: 2

Views: 12045

Answers (3)

Tarun Korat
Tarun Korat

Reputation: 11

I faced the same issue when I was using Inertia, Laravel, or Vue project. But i found the solution.

 exportData() {
            // Construct the download URL with query parameters
            const query = new URLSearchParams({
                search: this.searchQuery,
                sales_persons: this.searchSalesPerson,
                products: this.searchProduct,
                material_types: this.searchMaterial,
                export: 'true'
            }).toString();

            // Trigger the browser to navigate to the download URL
            window.location.href = this.route('module-certificate.index') + '?' + query;
},

Upvotes: 0

yurianxdev
yurianxdev

Reputation: 51

I think is a little bit late but I had the same problem and I just solved it.

For download a file from vuejs, you shouldn't make the request as an Axios petition (at least not that I know). Instead, you can use a href and download attribute inside your template.

This is an example:

<a
    type="button"
    href="/api/export/entity/"
    download="file.xlsx"
>
    <button
        @click="export()"
        class="btn btn-primary"
    >
    Export
    </button>
</a>

The method helps if you need to do another stuff.

I hope this help you.

Upvotes: 3

dhruvin prajapati
dhruvin prajapati

Reputation: 321

ok I solve the problem, I use Vue-excel-export package that will Export the Excel File

enter link description here

this link can help me to install package and use it

Upvotes: 2

Related Questions