69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Informe;
|
|
|
|
use PhpOffice\PhpSpreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
|
use Incoviba\Common\Define;
|
|
|
|
class Excel implements Define\Informe
|
|
{
|
|
protected string $title;
|
|
protected string $filename;
|
|
protected array $data;
|
|
|
|
public function setTitle(string $title): Excel
|
|
{
|
|
$this->title = $title;
|
|
return $this;
|
|
}
|
|
public function setFilename(string $filename): Excel
|
|
{
|
|
$this->filename = $filename;
|
|
return $this;
|
|
}
|
|
public function addData(array $rows): Excel
|
|
{
|
|
foreach ($rows as $row) {
|
|
$this->addRow($row);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addRow(array $row): Excel
|
|
{
|
|
foreach ($row as $cell) {
|
|
$this->addCell($cell);
|
|
}
|
|
return $this;
|
|
}
|
|
public function addCell(PhpSpreadsheet\Cell\Cell $cell): Excel
|
|
{
|
|
$this->data []= $cell;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
* @throws PhpSpreadsheet\Writer\Exception
|
|
*/
|
|
public function build(): void
|
|
{
|
|
$spreadsheet = new PhpSpreadsheet\Spreadsheet();
|
|
$spreadsheet->getProperties()
|
|
->setCreator('admin@incoviba.cl')
|
|
->setSubject($this->title)
|
|
->setCompany('Incoviba')
|
|
->setTitle($this->title);
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setTitle($this->title);
|
|
|
|
foreach ($this->data as $rowIndex => $row) {
|
|
foreach ($row as $columnIndex => $cell) {
|
|
$sheet->getCell([$columnIndex + 1, $rowIndex + 1])->setValue($cell);
|
|
}
|
|
}
|
|
|
|
$writer = PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
|
|
$writer->save($this->filename);
|
|
}
|
|
}
|