我正在使用maatwebsite Laravel Excel,而我正在尝试的是导入csv,然后在视图中显示结果。
我导入CSV然后转储结果没有问题:
object(Maatwebsite\Excel\Collections\RowCollection)#464 (2) {
["title":protected]=>
string(9) "Worksheet"
["items":protected]=>
array(3) {
[0]=>
object(Maatwebsite\Excel\Collections\CellCollection)#472 (2) {
["title":protected]=>
NULL
["items":protected]=>
array(2) {
["name"]=>
string(11) "Owen Kelley"
["age"]=>
float(29)
}
}
[1]=>
object(Maatwebsite\Excel\Collections\CellCollection)#473 (2) {
["title":protected]=>
NULL
["items":protected]=>
array(2) {
["name"]=>
string(9) "Jim Jones"
["age"]=>
float(50)
}
}
[2]=>
object(Maatwebsite\Excel\Collections\CellCollection)#474 (2) {
["title":protected]=>
NULL
["items":protected]=>
array(2) {
["name"]=>
string(10) "Sally Anne"
["age"]=>
float(35)
}
}
}
}
以下是我的控制器中的代码:
public function show()
{
$results = Excel::load('public\uploads\file.csv', function($reader) {
$reader->select(array('name', 'age'))->get();
});
return view('excel.show', compact('results'));
}
如前所述,如果我改变:return view('excel.show', compact('results'));
到
$results->dump();
然后我可以看到表格中的所有数据,但我不知道如何将这些数据传递给视图。
任何人都可以帮我吗?
我知道如果我正在努力完成这项任务,这是我应该知道的事情,但我仍然非常擅长编码,而且时间紧迫。
感谢。
答案 0 :(得分:1)
如果$results->dump()
将其传递给视图,则很可能也会有效。
我的猜测是Laravel在你的观点中找不到excel.show。你能告诉我们excel.show文件,并验证权限是否正确?
答案 1 :(得分:0)
试试这段代码:
$file= "test.xls";
$data = Excel::load($file, function($reader) {
foreach($reader->toArray()[0] as $row){
}
})->get()->toArray()[0];
return view('upload_load',compact('data'));