2017-04-27 76 views
1

我在Laravel使用maatwebsite/excel構建了一個excel文件。不過,它似乎是以不符合預期的方式在行之間添加數據。我是否錯過了可能導致這種情況的事情?它也不一致,例如對於新工作表,即使工作表左側可能有不同的數字,也會逐字添加相同的數據。Laravel Maatwebsite/excel隨機添加額外列中的數據

enter image description here

作爲一種解決辦法我有一堆空單元格添加到像這樣的每一行...

   $sheet->row(1, array(
        'From: '.$from_date, 
        'To: '.$to_date, 
        '', 
        '', 
        '', 
        '', 
        '', 
        '', 
        '', 
        '', 
        '' 
       )); 

我沒有解決辦法代碼:

$rows[$product_type] = array (
     'Received'    => $received, 
     'Visually Inspected' => $visually_inspected, 
     'Visually Passed'  => $visually_passed, 
     'Visually Failed'  => $visually_failed, 
     'Technically Inspected' => $technically_inspected, 
     'Technically Passed' => $technically_passed, 
     'Technically Failed Phase 1' => $technically_failed_1, 
     'Technically Failed Phase 2' => $technically_failed_2, 
     'Technically Failed Phase 3' => $technically_failed_3, 
     'Total Technically Failed' => $failed 
    ); 
    Excel::create('KGdfmg564fdsin4', function ($excel) use ($rows, $product_types, $to_date, $from_date) { 

     // Set the title 
     $excel->setTitle('Company Dashboard Report Data'); 

     // Chain the setters 
     $excel->setCreator('Company ') 
       ->setCompany('Company '); 
     foreach($product_types as $key => $val) { 
      $excel->sheet($key, function ($sheet) use ($rows, $key, $to_date, $from_date) { 

       $sheet->fromArray($rows); 
       $sheet->setWidth(array(
        'A' => 50, 
        'B' => 50 
       )); 
       $sheet->row(1, array(
        'From: '.$from_date, 
        'To: '.$to_date, 

       )); 
       $sheet->row(2, array(
        'Received', 
        $rows[$key]['Received'], 

       )); 
       $sheet->row(3, array(
        'Visually Inspected', 
        $rows[$key]['Visually Inspected'], 
       )); 
       $sheet->row(4, array(
        'Visually Passed', 
        $rows[$key]['Visually Passed'], 
       )); 
       $sheet->row(5, array(
        'Visually Failed', 
        $rows[$key]['Visually Failed'], 
       )); 
       $sheet->row(6, array(
        'Technically Inspected', 
        $rows[$key]['Technically Inspected'], 
       )); 
       $sheet->row(7, array(
        'Technically Passed', 
        $rows[$key]['Technically Passed'], 
       )); 
       $sheet->row(8, array(
        'Technically Failed Phase 1', 
        $rows[$key]['Technically Failed Phase 1'], 
       )); 
       $sheet->row(9, array(
        'Technically Failed Phase 2', 
        $rows[$key]['Technically Failed Phase 2'], 
       )); 
       $sheet->row(10, array(
        'Technically Failed Phase 3', 
        $rows[$key]['Technically Failed Phase 3'], 
       )); 
       $sheet->row(11, array(
        'Units Technically Failed', 
        $rows[$key]['Total Technically Failed'], 
       )); 


      }); 
     } 

     // Call them separately 
     $excel->setDescription('Dashboard Report Data'); 

    })->save('xlsx'); 

回答

0

從你的代碼中刪除$sheet->fromArray($rows);對我來說很有幫助。

enter image description here

+0

謝謝:),我有一種感覺,這是我正在做的事! – Adrian