Skip to content

Commit

Permalink
export multiple sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
rap2hpoutre committed Jul 10, 2018
1 parent 2534cdd commit 26b4da3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,52 @@ public function download($path, callable $callback = null)

/**
* @param $path
* @param string $function
* @param string $function
* @param callable|null $callback
*
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\InvalidArgumentException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException
* @throws \Box\Spout\Common\Exception\SpoutException
*/
private function exportOrDownload($path, $function, callable $callback = null)
{
$writer = WriterFactory::create($this->getType($path));
$this->setOptions($writer);
/* @var \Box\Spout\Writer\WriterInterface $writer */
$writer->$function($path);
if ($this->data instanceof Collection) {
// Apply callback
if ($callback) {
$this->data->transform(function ($value) use ($callback) {
return $callback($value);
});

$is_multi_sheet_writer = ($writer instanceof \Box\Spout\Writer\XLSX\Writer || $writer instanceof \Box\Spout\Writer\ODS\Writer);

// It can export one sheet (Collection) or N sheets (SheetCollection)
if ($this->data instanceof SheetCollection) {
$data = $this->data;
} else {
$data = collect([$this->data]);
}

foreach ($data as $key => $collection) {
if ($collection instanceof Collection) {
// Apply callback
if ($callback) {
$collection->transform(function ($value) use ($callback) {
return $callback($value);
});
}
// Prepare collection (i.e remove non-string)
$this->prepareCollection();
// Add header row.
if ($this->with_header) {
$first_row = $collection->first();
$keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray());
$writer->addRow($keys);
}
$writer->addRows($collection->toArray());
}
// Prepare collection (i.e remove non-string)
$this->prepareCollection();
// Add header row.
if ($this->with_header) {
$first_row = $this->data->first();
$keys = array_keys(is_array($first_row) ? $first_row : $first_row->toArray());
$writer->addRow($keys);
if ($is_multi_sheet_writer && $data->keys()->last() !== $key) {
$writer->addNewSheetAndMakeItCurrent();
}
$writer->addRows($this->data->toArray());
}
$writer->close();
}
Expand Down
17 changes: 17 additions & 0 deletions tests/FastExcelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rap2hpoutre\FastExcel\Tests;

use Rap2hpoutre\FastExcel\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;

/**
* Class FastExcelTest.
Expand Down Expand Up @@ -122,4 +123,20 @@ public function testExcelExportWithCallback()
);
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 testExportMultiSheetXLSX()
{
$file = __DIR__.'/test_multi_sheets.xlsx';
$sheets = new SheetCollection([clone $this->collection(), clone $this->collection()]);
(new FastExcel($sheets))->export($file);
$this->assertEquals($this->collection(), (new FastExcel())->import($file));
unlink($file);
}
}

0 comments on commit 26b4da3

Please sign in to comment.