gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:14:13 27 4
gpt4 key购买 nike

我正在使用 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.

我知道错误的意思,但是我怎么能用我的地址而不是这个库中的目录地址呢?

最佳答案

解决方案一

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

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

方案二

您也可以使用 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。

关于php - 如何在 laravel 中读取 xls 文件 - laravel-excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055421/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com