doutale7115
2017-06-23 16:56
浏览 308
已采纳

Laravel将数据导出到Excel文件中需要进行修改

I want to download 2 user's data from tables, I want to generate excel file of that data.

here is the download function for only one data table called registerdetails.

public function export(){
    $items = registerdetails::all();
    Excel::create('General Trainee Details', function($excel) use($items){
        $excel->sheet('Sheet 1', function($sheet) use($items){
            $sheet->fromArray($items);
        });
    })->export('xlsx');
}

I need this controller to be modified get datas from registerdetails and bankdetails. if anyone can help me to get this solved?

图片转代码服务由CSDN问答提供 功能建议

我想从表中下载2个用户的数据,我想生成该数据的excel文件。

这里只有一个名为 registerdetails 的数据表的下载功能。

  public function export(){
 $  items = registerdetails :: all(); 
 Excel :: create('General Trainee Details',function($ excel)use($ items){
 $ excel-> sheet('Sheet 1',function($  sheet)使用($ items){
 $ sheet-> fromArray($ items); 
}); 
}) - > export('xlsx'); 
} 
  <  / pre> 
 
 

我需要修改此控制器从 registerdetails bankdetails 获取数据。 如果有人能帮助我解决这个问题吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqfsbvd43312 2017-06-24 06:07
    已采纳

    try this

    public function collectionexport_all(){
        $items = registerdetails::join('bankdetails', 'bankdetails.id', '=', 'registerdetails.id')->get();
        $itemsArray = []; 
         foreach ($items as $item) {
            $itemsArray[] = $item->toArray();
        }
    
        Excel::create('Full Details', function($excel) use($itemsArray){
        $excel->sheet('Sheet 1', function($sheet) use($itemsArray){
    
            $sheet->fromArray($itemsArray);
    
        });
    
    })->export('xlsx');
    }
    
    打赏 评论
  • douxian7808 2017-06-23 22:43

    I'm not sure what you want to achieve.. But I hope this helps.

    The Excel Library you're using (hoping it's maatwebsite/excel or its like) can make use of Eloquent queries or Query Builder like so:

    public function export($id){
    
        Excel::create('General Trainee Details', function($excel) use($id){
            $excel->sheet('Sheet 1', function($sheet) use($id){
                $query= registerdetails::select(['registerdetailscolumn_1', 'registerdetailscolumn_2')->leftjoin('bankdetails', 'registerdetailscolumn_id', '=', 'bankdetailscolumn_id')->where(registerdetailscolumn_id, '=', $id);
                $sheet->fromModel($query);
            });
        })->export('xlsx');
    }
    

    Of course, knowing you can provide and pass along whatever employee's id it is.

    you can export one table on one sheet and another table on another sheet like so:

        public function export(){
    
            Excel::create('General Trainee Details', function($excel) use($id){
                $excel->sheet('Sheet 1', function($sheet){
                    $query= registerdetails::all();
                    $sheet->fromModel($query);
                });
                $excel->sheet('Sheet 2', function($sheet){
                    $query= bankdetails::all();
                    $sheet->fromModel($query);
                });
            })->export('xlsx');
        }
    
    打赏 评论

相关推荐 更多相似问题