如何在laravel中读取xls文件 - laravel-excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44055421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read xls file in laravel - laravel-excel
提问by S.M_Emamian
I'm using laravel-excel
library to read excel files.
我正在使用laravel-excel
库来读取 excel 文件。
http://www.maatwebsite.nl/laravel-excel/docs/import
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);
});
this file http://localhost:8000/assets/panel/excel/test123.xls
exist but I got this error:
该文件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.
I know the meaning of the error, but how can I use my address instead of directory address in this library?
我知道错误的含义,但是如何在这个库中使用我的地址而不是目录地址?
采纳答案by Dmytro Dzyubak
Solution 1
方案一
Just tested and the following should work:
刚刚测试,以下应该工作:
// /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 is based on PHPExcel code by PHPOffice
Laravel的Excel是基于PHPExcel代码PHPOffice
One of the examples from PHPExcel documentation has the following code: https://github.com/PHPOffice/PHPExcel/blob/1.8/Documentation/Examples/Reader/exampleReader01.php#L29
PHPExcel 文档中的示例之一具有以下代码:https: //github.com/PHPOffice/PHPExcel/blob/1.8/Documentation/Examples/Reader/exampleReader01.php#L29
Solution 2
解决方案2
You can use public_path()
Laravel helper function as well:
你也可以使用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);
});
});
Discussion
讨论
Portion of a file that generates an error:
生成错误的文件部分:
// /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.");
}
// ...
}
As You can see PHPExcel uses file_exists()
PHP function to check, if the file exists. file_exists()
can check local path only and not the remote path / URL.
如您所见,PHPExcel 使用file_exists()
PHP 函数来检查文件是否存在。file_exists()
只能检查本地路径,而不是远程路径/URL。