diff --git a/src/Exportable.php b/src/Exportable.php index cb2b7e3..7309347 100644 --- a/src/Exportable.php +++ b/src/Exportable.php @@ -109,6 +109,9 @@ private function exportOrDownload($path, $function, callable $callback = null) } $writer->addRows($collection->toArray()); } + if (is_string($key)) { + $writer->getCurrentSheet()->setName($key); + } if ($has_sheets && $data->keys()->last() !== $key) { $writer->addNewSheetAndMakeItCurrent(); } diff --git a/tests/IssuesTest.php b/tests/IssuesTest.php index 5617c80..f3214e5 100644 --- a/tests/IssuesTest.php +++ b/tests/IssuesTest.php @@ -2,8 +2,11 @@ namespace Rap2hpoutre\FastExcel\Tests; +use Box\Spout\Common\Type; +use Box\Spout\Reader\ReaderFactory; use Illuminate\Support\Collection; use Rap2hpoutre\FastExcel\FastExcel; +use Rap2hpoutre\FastExcel\SheetCollection; /** * Class IssuesTest. @@ -110,4 +113,24 @@ public function testIssue32() $this->assertEquals($original_collection[1], $res[1]); unlink(__DIR__.'/test2.xlsx'); } + + /** + * @throws \Box\Spout\Common\Exception\IOException + * @throws \Box\Spout\Common\Exception\InvalidArgumentException + * @throws \Box\Spout\Common\Exception\UnsupportedTypeException + * @throws \Box\Spout\Reader\Exception\ReaderNotOpenedException + * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException + */ + public function testIssue40() + { + $c = new SheetCollection(['1st Sheet' => $this->collection(), '2nd Sheet' => $this->collection()]); + (new FastExcel($c))->export(__DIR__.'/test2.xlsx'); + $reader = ReaderFactory::create(Type::XLSX); + $reader->open(__DIR__.'/test2.xlsx'); + foreach ($reader->getSheetIterator() as $k => $sheet) { + $this->assertEquals($sheet->getName(), $k === 2 ? '2nd Sheet' : '1st Sheet'); + } + $reader->close(); + unlink(__DIR__.'/test2.xlsx'); + } }