我尝试使用 Laravel Excel 实现从视图方法导出到 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>
贷款控制器.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');
}
}
网页.php
Route::get('/loanexport', 'LoansController@loanexport');