php - Laravel excel,水平阅读

标签 php excel laravel laravel-5.4

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

我的 Excel 文件如下所示 -

enter image description here

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

$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

编辑

$rows = Excel::load('storage\\app\\public\\upload\\Funiture.xlsx)->get();
<span>Furniture Registration Number : <b>{{ $rows[0][1] }}</b></span>

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

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

正确吗?

编辑

enter image description here

这是 dd(rows) 的结果。如何检索数组的数量?在本例中,如图所示,它有 7

最佳答案

正如 Laravel-Excel 文档所述:

By default the first row of the excel file will be used as attributes .

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

试试这个

   $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();

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

@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 行,然后使用 array_slice读取索引 [0][1] 处的值后跳过前 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

关于php - Laravel excel,水平阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45451099/

相关文章:

php - detach() 方法也可以应用于 Laravel 中的一对多关系吗?

laravel - vue-sass-loader 只编译样式标签,不编译文件

laravel - CamelCase类型 Controller 的 View 文件夹的命名约定是什么

php - 难以创建指向 MySQL 表中存储文件的链接

php - 无需 mod_rewrite 即可清理 PHP 中的 URL

php - Laravel 集合 : Shift Value *and* Key?

python - 将 MS Excel XML 文件读取到 pandas 数据框?

php - 分面搜索属性计数

vba - 对象不支持命名参数

vba - 使用 VBA 轻松通用打印字典到 Excel 工作表