我试图执行导出到Excel文件使用的Laravel Excel检视方法。这里是文档https://laravel-excel.maatwebsite.nl/3.1/exports/from-view.html的链接。但我不出来呢引用在网站上展示的例子。它返回一个错误说PhpOffice \ PhpSpreadsheet \ Writer \ Exception
Invalid parameters passed.
。我已经改变了我的代码试图在所有解决这个问题,但没有运气。请帮我想出解决办法。下面是我的代码。谢谢。
LoansExport.php
<?php
namespace App\Exports;
use App\Loan;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class LoansExport implements FromView
{
public function view(): View
{
return view('partials.view_loan_export', [
'loans' => Loan::all()
]);
}
}
view_loan_export.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
@foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
@endforeach
</tbody>
</table>
</body>
LoansController.php
<?php
namespace App\Http\Controllers;
use App\Loan as Loan;
use App\Member as Member;
use Illuminate\Http\Request;
use App\Exports\LoansExport;
use Maatwebsite\Excel\Facades\Excel;
class LoansController extends Controller
{
public function loanexport()
{
return Excel::download(new LoansExport, 'loans.xlsx');
}
}
web.php
Route::get('/loanexport', 'LoansController@loanexport');
只是把其中的table标签和标记在您的视图
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
@foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
@endforeach
</tbody>
</table>