当前位置: 首页 > 工具软件 > php-xls > 使用案例 >

php laravel导入excel,php-如何在laravel中读取xls文件-laravel-excel

郑帅
2023-12-01

我正在使用laravel-excel库读取Excel文件.

//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.

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

解决方法:

解决方案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代码

解决方案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.");

}

// ...

}

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

标签:laravel,laravel-5,laravel-excel,php,excel

来源: https://codeday.me/bug/20191111/2019298.html

 类似资料: