From 66fd83bd4a0bd3d1a8cc99bdc4d1750a1ad5aa7b Mon Sep 17 00:00:00 2001 From: reza Date: Tue, 17 Nov 2020 15:50:58 +0330 Subject: [PATCH 1/3] adds multisheet --- src/Contracts/ExportsExcel.php | 6 ++++-- src/Services/ExcelExport.php | 30 ++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/Contracts/ExportsExcel.php b/src/Contracts/ExportsExcel.php index cf00fa4..88b0e1d 100644 --- a/src/Contracts/ExportsExcel.php +++ b/src/Contracts/ExportsExcel.php @@ -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; } diff --git a/src/Services/ExcelExport.php b/src/Services/ExcelExport.php index c8f1bae..bd4366e 100644 --- a/src/Services/ExcelExport.php +++ b/src/Services/ExcelExport.php @@ -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; @@ -44,14 +45,17 @@ public function save(): string private function handle(): void { - $this->writer() - ->heading() - ->rows(); + $this->writer(); + + (new Collection($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) @@ -68,20 +72,30 @@ 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)); } From 3bc1b9e8b601fb86aee6b137c16293479453606e Mon Sep 17 00:00:00 2001 From: Mihai Date: Tue, 17 Nov 2020 12:21:17 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- src/Services/ExcelExport.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Services/ExcelExport.php b/src/Services/ExcelExport.php index bd4366e..a19540f 100644 --- a/src/Services/ExcelExport.php +++ b/src/Services/ExcelExport.php @@ -85,7 +85,6 @@ private function sheet(string $sheet, int $sheetIndex): self return $this; } - private function heading(string $sheet): self { $this->writer->addRow($this->row($this->exporter->heading($sheet))); From 2260a08f06093928517b56ec338ca912e1862c0a Mon Sep 17 00:00:00 2001 From: reza Date: Wed, 18 Nov 2020 15:48:08 +0330 Subject: [PATCH 3/3] addresses review --- src/Services/ExcelExport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/ExcelExport.php b/src/Services/ExcelExport.php index a19540f..9413885 100644 --- a/src/Services/ExcelExport.php +++ b/src/Services/ExcelExport.php @@ -47,7 +47,7 @@ private function handle(): void { $this->writer(); - (new Collection($this->exporter->sheets())) + Collection::wrap($this->exporter->sheets()) ->each(fn ($sheet, $sheetIndex) => $this->sheet($sheet, $sheetIndex) ->heading($sheet) ->rows($sheet));