Laravel Excel how to load a view to specific row or cell?

喜夏-厌秋 提交于 2019-12-22 04:15:11

问题


I'm using Laravel Excel, I want to load a view file and set it to specific row. Is it possible to do it with laravel excel? bellow is the code for loading view in to the sheet:

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

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

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

    });

});

回答1:


It seems like the package doesn't offer that I this time but maybe you could do something like this.

  • Create a wrapping view that you can pass some parameters to, such as a target row number, column number and inner view to include.

    Excel::create('New file', function($excel) {
    
        $excel->sheet('New sheet', function($sheet) {
    
           $sheet->loadView('path.to.wrapping.view')->with(["targetRow"=>5,"targetCol"=>10,"targetView"=>"path.to.target.view"]);
    
        });
    
    })->download();
    
  • Dynamically generate a table based on the parameters provided to the wrapping view. The content of the view could be something like this.

    @if(isset($targetRow) && isset($targetCol)) table tag @for ($row = 1; $row <= $targetRow + 1; $row++) tr tag @for ($col = 1; $col <= $targetCol + 1; $col++) td tag @if($row == $targetRow && $col == $targetCol) @include($targetView) @endif close td tag @endfor close tr tag @endfor close table tag @endif



来源:https://stackoverflow.com/questions/35428288/laravel-excel-how-to-load-a-view-to-specific-row-or-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!