将数组的数组从 phpexcel 传递到 blade laravel

Passing array of array from phpexcel to blade laravel

我想将数组的数组传递给 blade laravel,我正在使用 laravel 5.5 和 php 7

我的控制器:

  public function openexcel(Request  $request, $directory)
    {
        $data = Exceluploads::get();
        $path = 'excel/'.$directory;
        $objPHPExcel = PHPExcel_IOFactory::load($path);
        $sheet = $objPHPExcel->getSheetByName('mySheet1');

        $a = $sheet->rangeToArray('A1:D9');
    return view('excel.index_openexcel', compact('objPHPExcel,a'));
}

例如数据excel:

return$a

如何在blade中显示laravel

你可以试试这个。

<table>
    <thead>
        <tr>
            <th>{{ $objPHPExcel[0][0] }}</th>
            <th>{{ $objPHPExcel[0][1] }}</th>
            <th>{{ $objPHPExcel[0][2] }}</th>
            <th>{{ $objPHPExcel[0][3] }}</th>
        </tr>
    </thead>
    <tbody>
        @for($i=1; $i<count($objPHPExcel); $i++) 
            <tr>
                <td>{{ $objPHPExcel[$i][0] }}</td>
                <td>{{ $objPHPExcel[$i][1] }}</td>
                <td>{{ $objPHPExcel[$i][2] }}</td>
                <td>{{ $objPHPExcel[$i][3] }}</td>
            </tr>
        @endfor
    </tbody>
</table>

或者这样。

<table>
    <thead>
        <tr>
            @for($i=0; $i<count($objPHPExcel[0]); $i++) 
            <th>{{ $objPHPExcel[0][$i] }}</th>
            @endfor
        </tr>
    </thead>
    <tbody>
        @for($i=1; $i<count($objPHPExcel); $i++) 
            <tr>
                @for($j=0; $j<count($objPHPExcel[$i]); $j++) 
                <td>{{ $objPHPExcel[$i][$j] }}</td>
                @endfor
            </tr>
        @endfor
    </tbody>
</table>

您可以在 blade 文件中像这样循环数组。

<table>
  <thead>
    <tr>
      {{-- loop the column names --}}
      @foreach ($a[0] as  $columnName)
        <td>{{$columnName}}</td>
      @endforeach
    </tr>
  </thead>
  <tbody>
    {{-- loop all the arrays except the first on --}}
    @for ($i = 1; $i < count($a); $i++)
      <tr>
        {{-- get the data of that array --}}
        @foreach ($a[$i] as $data)
           <td>{{ $data }}</td>
        @endforeach
      </tr>
    @endfor
  </tbody>
</table>

希望对您有所帮助!