Laravel擅长,横向阅读

时间:2017-08-02 03:46:20

标签: php excel laravel laravel-5.4

目前使用Laravel-Excel(Maatwebsite)软件包上传excel并阅读它们。问题是,如何在同一个excel文件中读取水平和垂直数据?

我的excel文件看起来像这样 -

enter image description here

到目前为止,这是我的代码,但它没有读取任何内容

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.11</version>
</dependency>

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.11</version>
</dependency>

修改

$rows = Excel::load('storage\\app\\public\\upload\\Funiture.xlsx)->get();

@foreach ($rows as $key => $value)
    <tr>
      <td>{{ $value->{'Furniture Registration Number'} }}</td>
      <td>{{ $value->{'Number'} }}</td>
      <td>{{ $value->{'Chair Name'} }}</td>
      <td>{{ $value->{'Table Name'} }}</td>
      <td>{{ $value->{'Carpet Color'} }}</td>
   </tr>
@endforeach

这是对的吗?

修改

enter image description here

这是dd(行)的结果。如何检索数组量?在这种情况下,它有7,如图所示

1 个答案:

答案 0 :(得分:3)

正如Laravel-Excel文档所说:

  

默认情况下,excel文件的第一行将用作属性   

因此,如果要使用属性,则需要为第一行定义正确的标题。否则你可以做一件事,只是标题为

试试这个

   $rows= Excel::load("storage\\app\\public\\upload\\Funiture.xlsx", function($reader) {
       $reader->noHeading();
        $reader->skipRows(3); // skip first three rows as they contain headings
    })->get();
    $rows = $rows->toArray();

然后在您的视图中,您可以像这样循环遍历

@foreach ($rows as $key => $value)
    <tr>
      <td>{{ $value[0] }}</td>
      <td>{{ $value[1] }}</td>
      <td>{{ $value[2] }}</td>
      <td>{{ $value[3] }}</td>
   </tr>
    @endforeach

编辑:

您不需要读取文件两次只读取文件而不跳过3行,然后在读取索引[0][1]的值后使用array_slice跳过前3行。

     $rows= Excel::load("storage\\app\\public\\upload\\Funiture.xlsx", function($reader) {
           $reader->noHeading();
     })->get();
   $rows = $rows->toArray();

然后在你看来

<span>Furniture Registration Number : <b>{{ $rows[0][1] }}</b></span>
@php
    $rows = array_slice($rows,4);
@endphp

@foreach ($rows as $key => $value)
  <tr>
    <td>{{ $value->[0] }}</td>
    <td>{{ $value->[1] }}</td>
    <td>{{ $value->[2] }}</td>
    <td>{{ $value->[3] }}</td>
  </tr>
@endforeach