如何在Laravel中导出具有日期范围的Excel数据?

时间:2019-10-07 12:35:18

标签: javascript php ajax laravel

使用此代码,我可以搜索日期范围内的数据,并在视图文件中显示其总数。现在,我想使用Maatwebsite/Laravel-Excel包(来自查询)或任何其他好的方法来导出具有日期范围的数据。

有人可以帮我吗?

Blade View

<div class="panel-heading">
    <div class="row">
        <div class="col-md-5">Total Records - <b><span id="total_records"></span></b></div>
        <div class="col-md-5">
            <div class="input-group input-daterange">
                <input type="text" name="from_date" id="from_date" readonly class="form-control" />
                <div class="input-group-addon">to</div>
                <input type="text" name="to_date" id="to_date" readonly class="form-control" />
            </div>
        </div>
        <div class="col-md-2">
            <button type="button" name="filter" id="filter" class="btn btn-info btn-sm">Filter</button>
            <button type="button" name="refresh" id="refresh" class="btn btn-warning btn-sm">Refresh</button>
        </div>
    </div>
</div>

<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th width="6%">Owner Manager</th>
            <th width="6%">CSR Name</th>
            <th width="6%">CSR EID</th>
        </tr>
    </thead>
</table>

JQuery Code

$(document).ready(function () {
    var date = new Date();

    $('.input-daterange').datepicker({
        todayBtn: 'linked',
        format: 'yyyy-mm-dd',
        autoclose: true
    });

    var _token = $('input[name="_token"]').val();

    fetch_data();

    function fetch_data(from_date = '', to_date = '') {
        $.ajax({
            url: "{{ route('dr.fetch_data') }}",
            method: "POST",
            data: {
                from_date: from_date,
                to_date: to_date,
                _token: _token
            },
            dataType: "json",
            success: function (data) {
                var output = '';
                $('#total_records').text(data.length);
                for (var count = 0; count < data.length; count++) {
                    output += '<tr>';
                    output += '<td>' + data[count].owner_manager + '</td>';
                    output += '<td>' + data[count].csr_name + '</td>';
                    output += '<td>' + data[count].csr_eid + '</td>';
                }
                $('tbody').html(output);
            }
        })
    }

    $('#filter').click(function () {
        var from_date = $('#from_date').val();
        var to_date = $('#to_date').val();
        if (from_date != '' && to_date != '') {
            fetch_data(from_date, to_date);
        } else {
            alert('Both Date is required');
        }
    });

    $('#refresh').click(function () {
        $('#from_date').val('');
        $('#to_date').val('');
        fetch_data();
    });
});

Controller Code

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class DRController extends Controller
{
    function index()
    {
        return view('dr');
    }

    function fetch_data(Request $request)
    {
        if ($request->ajax()) {
            if ($request->from_date != '' && $request->to_date != '') {
                $data = DB::table('users_info')->whereBetween('ptdate', array($request->from_date, $request->to_date))->get();
            } else {
                $data = DB::table('users_info')->orderBy('ptdate', 'desc')->get();
            }
            echo json_encode($data);
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

您可以使用此Laravel Excel软件包非常有用

https://docs.laravel-excel.com/3.1/exports/

您可以使用此命令设置它:

composer require maatwebsite/excel

使用创建excel导出类:

php artisan make:export UsersExport --model=User

在UsersExport类中添加以下内容:

        <?php
    
    namespace App\Exports;
    
    use Illuminate\Support\Facades\DB;
    
    use Maatwebsite\Excel\Concerns\FromQuery;
    
    use Maatwebsite\Excel\Concerns\Exportable;
    
    use Maatwebsite\Excel\Concerns\WithHeadings;
    
    class PostsExport implements FromQuery, WithHeadings
    {
        /**
        * @return \Illuminate\Support\Collection
        */
       
        use Exportable;
    
        protected $from_date;
        protected $to_date;
    
        function __construct($from_date,$to_date) {
                $this->from_date = $from_date;
                $this->to_date = $to_date;
        }
    
        public function query()
        {
            $data = DB::table('users_info')
->whereBetween('pdate',[ $this->from_date,$this->to_date])
->select('ownermanager','crsname','crsaid')
->orderBy('id');
    
    
            return $data;
        }
    
        public function headings(): array
        {
            return [
                'Owner Manager',
                'CSR Name',
                'CSR Aid',
            ];
        }
    }

在您的控制器中:

    public function export(Request $request) 
    {

        $from_date=$request->from_date;
        $to_date = $request->to_date;


         return Excel::download(new usersExport($from_date,$to_date), 'excelname.xlsx');
}

别忘了添加:

use App\Exports\UsersExport;

use Maatwebsite\Excel\Facades\Excel;

最后找到该功能的路径:

Route::get('userinfo/export/', 'DRController@export')->name('drc.export');

现在我们是否称呼这个网址:

http://127.0.0.1:8000/userinfo/export?from_date=2020-07-01&to_date=2020-07-20

它将下载与您认为相同的Excel文件。