The Rock
The Rock

Reputation: 413

How to export data in table with pagination to excel in laravel

I want to export my data in table with pagination in laravel to excel how can? i use Maatwebsite in Github.

My table in blade with pagination

<form method="GET" action="{{route('excelexport')}}">
   <div style="float: right;margin-top: -12px;">
      <button type="submit" class="btn btn-outline-primary">
         <i class="icon-file-excel"></i> Export to Excel
      </button>
   </div>
</form>

<table cellpadding="10px" width="100%" class="table_str">
    <thead style="font-size: 11px;">
        <tr>
            <th>col1 </th>                    
            <th>col2 </th>
            <th>col3 </th>
            <th>col4 </th>
            <th>col5 </th>
            <th>col6 </th>
            <th>col7 </th>
       </tr>
    </thead>
    <tbody style="font-size: 11.5px;">
        @foreach($result as $vals)
            <tr scope="row" style="border-bottom: solid 1px #ccc">
                <td>>{{$vals->fixass_id}}</a></td>
                <td>{{$vals->brand_name}}</td>
                <td>{{$vals->model_name}}</td>
                <td>{{$vals->branch_name}}</td>
                <td>{{$vals->dep_name}}</td>
                <td>{{$vals->type}}</td>
                <td>{{$vals->condition}}</td>
            </tr>
        @endforeach
    </tbody>
</table>
{{ $result->links('pagination.default') }}

My route:

Route::get('excelexport','ReportController@createexcel')->name('excelexport');

My Controller

public function createexcel(){
   Excel::create('Report_Inventory', function ($excel) {
      $excel->sheet('All Record', function ($sheet) {       
        $sheet->rows(array(array('Fixasset', 'Service tag', 'Brand', 'Model', 'CPU', 'HDD', 'RAM', 'IP','Window','System Type','Fullname')
                ))->freezeFirstRow();    
            });
        })->export('xls');
    }

How can i export data in these table to excel?

Upvotes: 1

Views: 4854

Answers (1)

user320487
user320487

Reputation:

You create multiple sheets based on the size of the result. Example from the docs:

Excel::create('Filename', function($excel) {

    // Our first sheet
    $excel->sheet('First sheet', function($sheet) {

    });

    // Our second sheet
    $excel->sheet('Second sheet', function($sheet) {

    });

})->export('xls');

Setup a loop to export as many sheets as needed.

Using a blade file:

Excel::create('New file', function($excel) {

    $excel->sheet('New sheet', function($sheet) {

        $sheet->loadView('folder.view');

    });

 });

Update

You can create the download using the data in your database:

// in your controller...

// say you have a table 'items' and a model 'Item', then you would do
public function createexcel(){
    $items = Item::all();
    Excel::create('items', function($excel) use($items) {
        $excel->sheet('ExportFile', function($sheet) use($items) {
            $sheet->fromArray($items);
        });
    })->export('xls');
}

Upvotes: 1

Related Questions