我使用 laravel 8 和 maatwebsite 3.1,想通過單擊圖示下載(匯出)我的資料庫表作為 excel 檔案。正如我搜索的那樣,我們必須由工匠創建一個類,php artisan make:export blogExport --model=blog并在控制器中使用下載方法。Excel::download(new blogExport, 'filename.xlsx');. blogExport 包括 collection() 函式,我們應該在其中確定輸出內容。但是我想在控制器中寫入函式并確定我想要的輸出,直接在這個函式中而不是使用 blogExport 。是否可以 ?有一種方法“CREATE”可以使用,但它已經過時并且不受 maatwebsite 3.1 支持。任何答復表示贊賞。
uj5u.com熱心網友回復:
class ExcellExport implements FromCollection, WithHeadings, WithEvents, WithStrictNullComparison,ShouldAutoSize
{
use Exportable;
public function __construct($header, $query)
{
$this->header = $header;
$this->query = $query;
}
//======================================
public function headings(): array
{
return [$this->header];
}
//======================================
public function collection()
{
return collect($this->query);
}
}
然后像這樣傳遞您的查詢:
$query = Estate::where($inputs)->with('landlord', 'rented', 'my_estate_list');
$headers=["code,type_caption,space,landlord,area,address,price"];
$resultExcell = new ExcellExport($headers, $query);
return Excel::download($resultExcell, 'file_name.xlsx');
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/399059.html
上一篇:對單元格進行評論
