将数组数组从phpexcel传递到刀片laravel

时间:2018-07-03 02:15:37

标签: php laravel phpexcel

我想将数组数组传递给刀片laravel,即时消息使用laravel 5.5和php 7

mycontroller:

  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:

enter image description here

返回$ a enter image description here

如何在叶片幼虫中显示它

2 个答案:

答案 0 :(得分:0)

您可以尝试一下。

<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>

答案 1 :(得分:0)

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

<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>

希望这会有所帮助!