spreadsheet = new Spreadsheet(); return $this; } protected function formatArray(array $data) { $maxCols = 0; foreach ($data as $row) { if (!is_array($row)) { continue; } if (count($row) > $maxCols) { $maxCols = count($row); } } foreach ($data as &$row) { if (!is_array($row)) { $row = [$row]; } if (count($row) < $maxCols) { $row = array_merge($row, array_fill(0, $maxCols - count($row), '')); } } return $data; } public function addArray(array $data, string $start = 'A1') { $this->spreadsheet->getActiveSheet()->fromArray($this->formatArray($data), null, $start); return $this; } public function send(string $filename) { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header("Content-Disposition: attachment;filename=\"{$filename}\""); header('Cache-Control: max-age=0'); $writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx'); $writer->save('php://output'); } }