Files
oficial/app/Service/Informe/Informe.php
2022-03-07 22:38:29 -03:00

56 lines
1.6 KiB
PHP

<?php
namespace App\Service\Informe;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
class Informe
{
protected $spreadsheet;
public function createSpreadsheet()
{
$this->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 formatColumn(string $column)
{
$this->spreadsheet->getActiveSheet()->getStyle("{$column}:{$column}")->setFormatCode('#,##0');
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');
}
}