Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/adds multi sheet #5

Merged
merged 3 commits into from Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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