gpt4 book ai didi

php - Laravel excel,水平阅读

转载 作者:行者123 更新时间:2023-12-02 04:16:10 25 4
gpt4 key购买 nike

目前使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com