【问题标题】:Laravel Excel Export - From View not workingLaravel Excel 导出 - 从视图不起作用
【发布时间】:2019-01-23 17:25:38
【问题描述】:

我尝试使用 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>

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');

错误

【问题讨论】:

  • 任何帮助请:(

标签: laravel laravel-5.7 maatwebsite-excel laravel-excel phpoffice


【解决方案1】:

我以不同的方式完成了

LoansController.php

public function loanexport(){
$loan= array();
    $loans= loan::all();
    $data =  [
        'success' => 'success',
        'loans' => $loans,
];
return Excel::download(new LoansExport($data), 'loans.xlsx');
}

LoansExport.php

public function __construct($data) {
    $this->data = $data;
}

public function view(): View
{
    //dd($this->data);   
    return view('partials.view_loan_export',$this->data);
}

view_loan_export.blade.php

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

【讨论】:

  • 你让我的一天更轻松
【解决方案2】:

只需将表格标签和其中的标签放在您的视图中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-15
    • 2021-12-29
    • 2019-04-23
    • 1970-01-01
    • 2020-07-18
    • 2017-06-18
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多