【问题标题】:How to export data in table with pagination to excel in laravel如何在laravel中使用分页将表格中的数据导出到excel
【发布时间】:2018-02-02 03:45:21
【问题描述】:

我想在 laravel 中使用分页将表格中的数据导出到 excel 怎么做?我在 Github 中使用 Maatwebsite。

我在刀片中的分页表

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

我的路线:

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

我的控制器

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

如何将这些表中的数据导出到excel?

【问题讨论】:

    标签: php laravel laravel-5.3 maatwebsite-excel


    【解决方案1】:

    您可以根据结果的大小创建多张工作表。文档中的示例:

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

    设置循环以根据需要导出尽可能多的工作表。

    使用刀片文件:

    Excel::create('New file', function($excel) {
    
        $excel->sheet('New sheet', function($sheet) {
    
            $sheet->loadView('folder.view');
    
        });
    
     });
    

    更新

    您可以使用数据库中的数据创建下载:

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

    【讨论】:

    • 我只需要一张纸,我想像上面的刀片一样将我的数据加载到 excel 中
    • 我想把我在中的数据导出到excel怎么做?
    • 使用刀片支持,可以直接加载视图文件:maatwebsite.nl/laravel-excel/docs/blade
    • 但我的视图是带参数的,当我在同一个视图中单击导出时,我想加载
    • 您使用完全专用于创建导出的不同视图文件。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签