无法打开阅读!文件不存在Phpexcel与laravel

时间:2014-11-30 04:18:15

标签: laravel-4

当我使用phpexcel-laravel时,我有读取文件.xls的问题。我在这里使用phpexcel https://github.com/Maatwebsite/Laravel-Excel,我想读取文件.xls中的数据。我的.xls有2列,列id和列名,我运行函数getFile,我收到消息错误是“无法打开阅读!文件不存在”。我不知道如何解决这个问题。你能指导我吗?谢谢!

我的控制器:

public function getFile(){
    $results=array();    
    Excel::load('file/test.xls', function($reader) {
        $results = $reader->select(array('id', 'name'))->get()->toArray();
    });

    $data = array(
        'results' = $results;
    );
   return View::make('show_excel',$data)->render();
 }

我的观点:

@if(!empty($results)) 
    <table class="table table-striped table-bordered">
        <thead>
        <tr>
            <th>ID</th>
            <th>name</th>
        </tr>
        </thead>
        @foreach ($results as $row)
        <tr>
            <td>{{$row->id}}</td>
            <td>{{$row->name}}</td>
        </tr>
        @endforeach 
    </table> 
@endif

1 个答案:

答案 0 :(得分:1)

public function getFile(){
    $data = Excel::load('file/test.xls', function($reader) {})->get();

    if(!empty($data) && $data->count()){
        foreach ($data->toArray() as $key => $value) {
            $insert[] = ['id' => $value['id'], 'name' => $value['name']];
        }

        if(!empty($insert)){
            Item::insert($insert);
            return back()->with('success','Insert Record successfully.');
        }
    }
}

使用完整文件路径。