[Laravel-Excel中文文档] maatwebsite/excel导出多个Sheet
Song •
2971 次浏览 •
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中distinct()的使用方法与去重 2017-09-11 -
Laravel将view缓存为静态html,laravel页面静态缓存 2021-10-09 -
[ laravel爬虫实战--基础篇 ] guzzle描述与安装 2017-11-01 -
[ 配置教程 ] 在ubuntu16.04中部署LNMP环境(php7+maridb且开启maridb远程以及nginx多域名访问 )并配置laravel环境 2017-07-18 -
nginx如何查看当前访问的是哪个网址 2025-11-17
热门文章
-
nginx如何查看当前访问的是哪个网址 2025-11-17 -
微信公众号回复菜单点击回复文本 2025-10-24 -
laravel+easywechat6出现No component_verify_ticket found以及修改缓存为redis 2025-09-09 -
nginx 服务器如何查看当前访问的域名 2025-06-10 -
ubuntu+nginx当服务器异常时微信/企业微信/抖音数据重复回调导致服务器崩溃怎么解决? 2025-05-30

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