【问题标题】:Could not open for reading! File does not exist Phpexcel with laravel无法打开阅读!文件不存在phpexcel与laravel
【发布时间】:2014-11-30 04:18:14
【问题描述】:

我在使用 phpexcel-laravel 时遇到了读取文件 .xls 的问题。我在这里https://github.com/Maatwebsite/Laravel-Excel 使用phpexcel,我想读取文件 .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

【问题讨论】:

  • 您是否尝试过将文件的完整路径传递给 Excel::load()?

标签: laravel-4


【解决方案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.');
        }
    }
}

使用完整的文件路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 2018-08-25
    相关资源
    最近更新 更多