【问题标题】:Export automatically in laravel excel在 laravel excel 中自动导出
【发布时间】:2020-01-10 13:38:09
【问题描述】:

我有一个疑问,我不知道如何发送参数,以便我的导出的收集功能使用该数据生成一个 excel。 我通过我的一个控制器的方法生成此查询

控制器是GenerateNumbersController.php,方法是validateNumberBD

我生成的查询如下:

$searchOrCreate = Phone::insertIgnore($consultArray);

if ($searchOrCreate) {
    $phones = Phone::select('PHONES.PHONE','AREACODES.CODE')
    ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
    ->where('DATE',$searchOrCreate)
    ->get();
}

该查询会生成一组数据,我需要发送这些数据以自动导出。

这是我的问题开始的地方,因为我不知道如何将该变量发送到 PhonesExport 类,该类生成导出到 Exce 的类

我尝试在 PhonesExport 类中创建一个名为 data ($ value) 的新函数,如下所示:

<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromCollection;
use App\Http\Controllers;

class PhonesExport implements FromCollection
{   
    public $data;

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        global $data;       
        return $data;
    }

    public function data($dato)
    {
        global $data;
        $data = $dato;
    }
}

并通过第一个代码的方法尝试生成该类的实例,然后通过相同的参数发送如下。

$searchOrCreate = Phone::insertIgnore($consultArray);

if ($searchOrCreate) {
    $phones = Phone::select('PHONES.PHONE','AREACODES.CODE')
    ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
    ->where('DATE',$searchOrCreate)
    ->get();    

    $excel = new PhonesExport();
    $excel->data($phones );

    \Excel::download($excel,'phones.xlsx');
}

但是在执行时它不会对我产生任何影响。

【问题讨论】:

    标签: laravel eloquent laravel-5.8 laravel-excel


    【解决方案1】:

    选项 1:使用依赖注入。

    $searchOrCreate = Phone::insertIgnore($consultArray);
    if ($searchOrCreate) {
        $phones = Phone::select('PHONES.PHONE','AREACODES.CODE')
        ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
        ->where('DATE',$searchOrCreate)
        ->get();    
    
        $excel = new PhonesExport($phones);
    
        \Excel::download($excel,'phones.xlsx');
    }
    
    namespace App\Exports;
    
    use Maatwebsite\Excel\Concerns\FromCollection;
    use Illuminate\Support\Collection;
    
    class PhonesExport implements FromCollection
    {
        protected $phones;
    
        public function __construct($phones)
        {
           $this->phones = $phones;
        }
    
        /**
         * @return Collection
         */
        public function collection()
        {
           return $this->phones;
        }
    }
    

    选项 2:将查询移至 Exporter 类。

    $searchOrCreate = Phone::insertIgnore($consultArray);
    if ($searchOrCreate) {    
        $excel = new PhonesExport($searchOrCreate);
    
        \Excel::download($excel,'phones.xlsx');
    }
    
    namespace App\Exports;
    
    use Maatwebsite\Excel\Concerns\FromCollection;
    use Illuminate\Support\Collection;
    use App\Phone;
    
    class PhonesExport implements FromCollection
    {
        protected $date;
    
        public function __construct($date)
        {
           $this->date = $date;
        }
    
        /**
         * @return Collection
         */
        public function collection()
        {
           return Phone::select('PHONES.PHONE','AREACODES.CODE')
           ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
           ->where('DATE', $this->date)
           ->get();
        }
    }
    

    选项 3:使用 readfile 强制下载。

    不要调用\Excel::download(...),而是调用\Excel::store(...),然后将下载返回到文件。

    // \Excel::download($excel,'phones.xlsx');
    \Excel::store($excel, 'tmp.xlsx');
    $filename = 'phones.xlsx';
    $filepath = storage_path('app/tmp.xlsx');
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filepath));
    flush(); // Flush system output buffer
    readfile($filepath);
    exit;
    
    namespace App\Exports;
    
    use Maatwebsite\Excel\Concerns\FromCollection;
    use Illuminate\Support\Collection;
    use App\Phone;
    
    class PhonesExport implements FromCollection
    {
        protected $date;
    
        public function __construct($date)
        {
           $this->date = $date;
        }
    
        /**
         * @return Collection
         */
        public function collection()
        {
           return Phone::select('PHONES.PHONE','AREACODES.CODE')
           ->join('AREACODES','AREACODES.AREACODES_ID','=','PHONES.AREACODES_ID')
           ->where('DATE', $this->date)
           ->get();
        }
    }
    

    【讨论】:

    • 不,它不会下载我。如果我做 dd 如果它告诉我我需要什么和一切。但它不会让我下载文件,只有当我按下按钮下载时。
    • 不行,他必须在代码运行时自动下载。如果我完成执行代码并在下载时给出下载按钮,但这不是所需要的。
    • 为什么?您是尝试通过 javascript 还是控制台下载?
    • 我仍然不确定我是否理解您为什么要这样做,但我会编辑另一个替代方案
    • 我稍微解释一下。我的应用程序是一个数字生成器,当它生成这些数字并将它们插入数据库时​​,我必须自动导出生成的 excel 文件,目前它不会自动下载文件,如果它下载当且仅当我按下 a按钮下载
    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    相关资源
    最近更新 更多