Skip to content

Commit

Permalink
Merge pull request #5 from laravel-enso/feature/addsMultiSheet
Browse files Browse the repository at this point in the history
Feature/adds multi sheet
  • Loading branch information
raftx24 committed Nov 18, 2020
2 parents ee4e409 + 2260a08 commit 31ad295
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/Contracts/ExportsExcel.php
Expand Up @@ -6,7 +6,9 @@ interface ExportsExcel
{
public function filename(): string;

public function heading(): array;
public function heading(string $sheet): array;

public function rows(): array;
public function rows(string $sheet): array;

public function sheets(): array;
}
29 changes: 21 additions & 8 deletions src/Services/ExcelExport.php
Expand Up @@ -6,6 +6,7 @@
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\XLSX\Writer;
use Illuminate\Support\Collection;
use LaravelEnso\Excel\Contracts\ExportsExcel;
use LaravelEnso\Excel\Contracts\SavesToDisk;
use LaravelEnso\Excel\Exceptions\ExcelExport as Exception;
Expand Down Expand Up @@ -44,14 +45,17 @@ public function save(): string

private function handle(): void
{
$this->writer()
->heading()
->rows();
$this->writer();

Collection::wrap($this->exporter->sheets())
->each(fn ($sheet, $sheetIndex) => $this->sheet($sheet, $sheetIndex)
->heading($sheet)
->rows($sheet));

$this->writer->close();
}

private function writer(): self
private function writer()
{
$defaultStyle = (new StyleBuilder())
->setShouldWrapText(false)
Expand All @@ -68,20 +72,29 @@ private function writer(): self
}

$this->writer->openToFile($this->filePath());
}

private function sheet(string $sheet, int $sheetIndex): self
{
if ($sheetIndex > 0) {
$this->writer->addNewSheetAndMakeItCurrent();
}

$this->writer->getCurrentSheet()->setName($sheet);

return $this;
}

private function heading(): self
private function heading(string $sheet): self
{
$this->writer->addRow($this->row($this->exporter->heading()));
$this->writer->addRow($this->row($this->exporter->heading($sheet)));

return $this;
}

private function rows(): self
private function rows(string $sheet): self
{
foreach ($this->exporter->rows() as $row) {
foreach ($this->exporter->rows($sheet) as $row) {
$this->writer->addRow($this->row($row));
}

Expand Down

0 comments on commit 31ad295

Please sign in to comment.