我正在使用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.我知道这个错误的含义,但是我如何在这个库中使用我的地址而不是目录地址?
发布于 2017-05-19 10:25:45
解决方案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.");
}
// ...
}正如您所看到的,PHPExcel使用file_exists() PHP函数来检查文件是否存在。file_exists()只能检查本地路径,而不能检查远程路径/ URL。
https://stackoverflow.com/questions/44055421
复制相似问题