使用 laravel 和 vuejs 导出 Excel

问题描述 投票:0回答:3

我很喜欢使用 Laravel 和 Vuejs 导出 Excel,不知何故,代码返回真实值,但无法下载 Excel 文件,如果我执行正常请求,它将下载文件,但在 axios 请求中,它不会导出文件

我正在使用 php artisan make:export 导出文件

在应用程序/导出/studentexport.php中

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

然后在控制器中我将执行一个功能

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

在我的 Vue 文件中,我将编写一段代码来调用控制器并导出文件

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

但是结果是这样的

这将返回True,我真的不知道如何解决这个问题,请帮助我

laravel vue.js laravel-5
3个回答
3
投票

我认为有点晚了,但我遇到了同样的问题,我刚刚解决了它。

要从 vuejs 下载文件,您不应该以 Axios 请愿形式提出请求(至少我不知道)。相反,您可以在模板中使用 href 和 download 属性。

这是一个例子:

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

如果您需要做其他事情,该方法会有所帮助。

我希望这对你有帮助。


2
投票

好的我解决了问题,我使用 Vue-excel-export 包来导出 Excel 文件

在此输入链接描述

这个链接可以帮助我安装包并使用它


0
投票

我在使用 Inertia、Laravel 或 Vue 项目时遇到了同样的问题。 但我找到了解决方案。

 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;
},
© www.soinside.com 2019 - 2024. All rights reserved.