dongshuming7131 2016-05-01 17:55
浏览 130
已采纳

laravel excel导出所有记录失败

i want to export all records from view into excel sheet using maatweb package

in my controller i used the following function

public function exportAll()
    {
Excel::create('POs', function ($excel)

        {
            $excel->setTitle('Pos');
            $excel->sheet('POs', function ($sheet)

            {

                $pos = PO::all();
                $arr =array();

                                foreach($pos as $po)
                                {
                                    foreach ($po->mr as $m)
                                        foreach ($po->suppliers as $supplier)

                                        {
                                            $data = array($m->mr_no, $po->po_no, $po->po_subject,
                                                $po->po_issued, $po->po_total_cost, $po->po_currency,
                                                $po->po_purchase_method, $po->po_payment_method,
                                                $po->po_delivery_method, $po->po_confirmation,
                                                $po->po_loaded_on_ideas, $supplier->vname,
                                                $po->po_loaded_on_ideas, $po->po_approved_on_ideas,
                                                $po->memo_to_fin, $po->po_delivery_date,
                                                $po->po_mr_received_date, $po->po_mrr_received_date,
                                                $po->po_invoice_received_date, $po->po_penalty,
                                                $po->po_cover_invoice, $po->po_completed

                                            );
                                            array_push($arr, $data);


                                        }
                                }

                                    $sheet->loadView( 'pos.pos_all_template' )->with('pos',$pos);
              //

                                           });
                    })->export('xlsx');
    } 

and in route file i used
Route::get('po_s/exportall','POsController@exportAll');

in my view file

@foreach($pos as $p)
@foreach($p->mr as $m)
@foreach($p->suppliers as $s)

<tr style="color:rgb(0, 0, 0);" align="center" >

    <th align="center" > {{ $m['mr_no'] }} </th>
    <th align="center" > {{ $p['po_no'] }} </th>
    <th align="center" > {{ $p['po_subject'] }} </th>
    <th align="center" >{{ $p['po_issued'] }} </th>
    <th align="center" >{{ $s['vname'] }} </th>
    <th align="center" >{{ $p['po_total_cost'] }} </th>
    <th align="center" >{{ $p['po_currency'] }} </th>
    <th align="center" >{{ $p['po_purchase_method'] }} </th>
    <th align="center" >{{ $p['po_payment_method'] }} </th>
    <th align="center" >{{ $p['po_delivery_method'] }} </th>
    <th align="center" >{{ $p['po_delivery_date'] }} </th>
    <th align="center" >{{ $p['po_loaded_on_ideas'] }} </th>
    <th align="center" >{{ $p['po_mr_received_date'] }} </th>
    <th align="center" >{{ $p['po_mrr_received_date'] }} </th>
    <th align="center" >{{ $p['po_invoice_received_date'] }} </th>
    <th align="center" >{{ $p['po_penalty'] }} </th>
    <th align="center" >{{ $p['po_cover_invoice'] }} </th>
    <th align="center" >{{ $p['po_completed'] }} </th>



</tr>
@endforeach
@endforeach
@endforeach

the problem i the receive empty excel file without any data

  • 写回答

1条回答 默认 最新

  • duanchuli5647 2016-05-02 03:42
    关注

    first try this - > dd( $pos = PO::all();); or enter code heredd($pos); if you get output then try your for-each loop what you are getting when you use dd();. if you get data then Creating Excel sheet is the problem check below code how im creating sheet

        public function createDocs()
            {
       \Excel::create('JOBS', function($excel) {
         $excel->sheet('2015', function($sheet) {
           $jobs = DB::table('jobs')->get();
           $data = array('JOB ID','CONSIGNEE CODE','CONSIGNEE NAME','CONSIGNEE VAT NO','DESCRIPTION','VESSEL & VOYAGE','INVOICE VALUE','ETA/ATA DATE','B/L or AWB NUMBER','COMPLETE DATE','DELIVERY INVOICE VALUE','DUTY AMOUNT','TERMINAL','PAYMENT','INCOTERMS','COM INVOICE NO','COM INVOICE DATE','COM INVOICE VALUE','BANK','BANKREFNO','ORIGIN','EXPORT','TRADING','TOTAL EXPENSES','PROFIT');
        $sheet->fromArray(array($data),null,'A1',false,false);
    
    foreach($jobs as $row){
         $sum = DB::table('charges')->where('job_id', $row->id)->sum('amount');
         $consignee = DB::table('consignees')->where('id', $row->id)->first();
                            $data=array($row->id,$consignee->code,$consignee->name,$consignee->vat_num,$row->description,$row->vessel,
                                $row->invoice_value,$row->eta_date,$row->awb_num,$row->complete_date,$row->del_inv_val,$row->duty_amount,
                                $row->terminal,$row->payment,$row->incoterm,$row->com_inv_no,$row->com_inv_date,$row->com_inv_value,$row->bank,$row->bank_ref_no,
                                $row->country_origin,$row->country_export,$row->country_trading,$sum,$row->invoice_value-$sum);
                            $sheet->fromArray(array($data),null,'A1',false,false);
    }});})->download('csv');}
    

    and use this link http://www.maatwebsite.nl/laravel-excel/docs/export

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)