所以我设法用Laravel-excel从学生表中创建一个excel文件,但是,我想把它分成小的工作表,代表20-25名学生的单独小组。
到目前为止,我只找到了方法,将其分为工作表基于一个条件,如一个组的东西,但不是基于一些记录为每个工作表
您可以考虑这样做:
创建StudentExport类:
namespace AppExports;
use AppModelsStudent;
use MaatwebsiteExcelConcernsExportable;
use MaatwebsiteExcelConcernsWithMultipleSheets;
class StudentExport implements WithMultipleSheets
{
use Exportable;
/**
* @return array
*/
public function sheets(): array
{
$sheets = [];
$total = Student::count();
$maxPage = (int) ceil($total/25);
for ($page = 1; $page <= $maxPage; $page++) {
$sheets[] = new StudentPerSheet($page);
}
return $sheets;
}
}
创建一个StudentPerSheet类
namespace AppExports;
use AppModelsStudent;
use MaatwebsiteExcelConcernsFromQuery;
use MaatwebsiteExcelConcernsWithTitle;
class StudentPerSheet implements FromQuery, WithTitle
{
private $page;
public function __construct(int $page)
{
$this->page = $page;
}
public function query()
{
return Student
::query()
->offset(($this->page - 1) * 25)
->limit(25);
}
/**
* @return string
*/
public function title(): string
{
return 'Page ' . $this->page;
}
}
:
public function downloadStudents()
{
return (new StudentExport())->download('students.xlsx');
}