使用Laravel与Vue.js进行Excel导出 [英] Excel Export using laravel with vuejs

查看:118
本文介绍了使用Laravel与Vue.js进行Excel导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Vuejs的Laravel进行Excel导出,代码以某种方式返回了真值,但无法下载Excel文件,如果我正常请求,它将下载文件,但是在axios请求中,它将不会导出文件

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

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

I am using php artisan make:export to export file

在App/Export/studentexport.php中

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');
}

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

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'
                        })
            })

但是结果是这样的

那将返回True,我真的不知道该如何解决,请帮帮我

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

推荐答案

我认为有点晚了,但是我遇到了同样的问题,我才解决了.

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

要从vuejs下载文件,您不应将请求作为Axios请愿书提出(至少不是我所知道的).相反,您可以在模板中使用href和download属性.

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.

这是一个示例:

<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.

希望这对您有所帮助.

这篇关于使用Laravel与Vue.js进行Excel导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆