[Laravel-Excel中文文档] maatwebsite/excel导出多个Sheet
Song •
3228 次浏览 •
0个评论 •
2022年03月21日
为了允许导出有多个工作表,WithMultipleSheets应该使用关注点。该sheets()方法需要返回一个工作表导出对象数组。
namespace App\Exports;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class InvoicesExport implements WithMultipleSheets
{
use Exportable;
protected $year;
public function __construct(int $year)
{
$this->year = $year;
}
/**
* @return array
*/
public function sheets(): array
{
$sheets = [];
for ($month = 1; $month <= 12; $month++) {
$sheets[] = new InvoicesPerMonthSheet($this->year, $month);
}
return $sheets;
}
}工作表类
InvoicesPerMonthSheet可以实现诸如FromQuery, FromCollection, ...之类的问题
namespace App\Exports\Sheets;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithTitle;
class InvoicesPerMonthSheet implements FromQuery, WithTitle
{
private $month;
private $year;
public function __construct(int $year, int $month)
{
$this->month = $month;
$this->year = $year;
}
/**
* @return Builder
*/
public function query()
{
return Invoice
::query()
->whereYear('created_at', $this->year)
->whereMonth('created_at', $this->month);
}
/**
* @return string
*/
public function title(): string
{
return 'Month ' . $this->month;
}
}这将下载当年所有发票的 xlsx,其中 12 个工作表代表一年中的每个月。
public function download()
{
return (new InvoicesExport(2018))->download('invoices.xlsx');
}
-
[Laravel-Excel中文文档] maatwebsite/excel映射处理导出数据以及设置导出标题 2022-03-21 -
[Laravel-Excel中文文档] 使用queued队列实现后端分批次导出大量数据 2022-03-21 -
[Laravel-Excel中文文档] Laravel从Modal数据中query查询数据并导出 2022-03-19 -
JS/Jquery/Vue使用localResizeIMG插件PC/移动端本地图片压缩 2022-03-08 -
[Laravel-Excel中文文档] 实现Excel/CSV文件导出教程 2022-03-05

所以SheetJS实现table导出数据为Excel