5

我正在使用laravel-excel库来读取 excel 文件。

http://www.maatwebsite.nl/laravel-excel/docs/import

   //http://localhost:8000/assets/panel/excel/test123.xls
    $address = URL::to('/assets/panel/excel/').'/test123.xls';
   // dd($address);
    Excel::load($address, function($reader) {

        $results = $reader->get();
        dd($results);

    });

此文件http://localhost:8000/assets/panel/excel/test123.xls存在,但我收到此错误:

Could not open C:\xampp\htdocs\tahrircenter\http://localhost:8000/assets/panel/excel/test123.xls for reading! File does not exist.

我知道错误的含义,但是如何在这个库中使用我的地址而不是目录地址?

4

1 回答 1

4

解决方案 1

刚刚测试过,以下应该可以工作:

// /routes/web.php
Route::get('excel-test', function () {
    // http://localhost/assets/panel/excel/test123.xls
    // /public/assets/panel/excel/test123.xls
    $address = './assets/panel/excel/test123.xls';
    Excel::load($address, function($reader) {
        $results = $reader->get();
        dd($results);
    });
});

Laravel Excel 基于 PHPOffice 的 PHPExcel代码

PHPExcel 文档中的示例之一具有以下代码: https ://github.com/PHPOffice/PHPExcel/blob/1.8/Documentation/Examples/Reader/exampleReader01.php#L29

解决方案 2

你也可以使用public_path()Laravel 辅助函数:

Route::get('excel-test', function () {
    $address = public_path('assets/panel/excel/test123.xls');
    Excel::load($address, function($reader) {
        $results = $reader->get();
        dd($results);
    });
});

讨论

产生错误的文件部分:

// /vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/Excel5.php
public function canRead($pFilename)
{
    // Check if file exists
    if (!file_exists($pFilename)) {
        throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
    }
    // ...
}

如您所见file_exists(),PHPExcel 使用 PHP 函数来检查文件是否存在。file_exists()只能检查本地路径而不是远程路径/ URL。

于 2017-05-19T02:25:44.523 回答