Skip to content

Instantly share code, notes, and snippets.

@doekenorg
Last active July 3, 2019 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doekenorg/b7fdfca59a98e2d36ebca0785d333976 to your computer and use it in GitHub Desktop.
Save doekenorg/b7fdfca59a98e2d36ebca0785d333976 to your computer and use it in GitHub Desktop.
Move all sheet data to a single sheet.
<?php
use GFExcel\Renderer\PHPExcelMultisheetRenderer;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class MultiFormSingleSheetRenderer extends PHPExcelMultisheetRenderer
{
public function renderOutput($extension = 'xlsx', $save = false)
{
$this->assembleWorksheets();
parent::renderOutput($extension, $save);
}
/**
* Retrieves all data from worksheets, and copies it to a single worksheet.
* N.B. It will lose all styling and column widths.
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
protected function assembleWorksheets()
{
$assemebled = new Worksheet();
$line = 1;
foreach ($this->spreadsheet->getWorksheetIterator() as $index => $worksheet) {
// copy all rows to new worksheet
$assemebled->fromArray($worksheet->toArray(), null, 'A' . $line);
// Keep track of row number for next iteration
$line += $worksheet->getHighestRow();
// Unescape this if you want 1 emtpy line between data sets.
// $line++;
}
// lose all worksheets
$this->spreadsheet->disconnectWorksheets();
// Add the new worksheet to the spreadsheet
$this->spreadsheet->addSheet($assemebled, 0);
$this->spreadsheet->setActiveSheetIndex(0);
}
}
@valentin-anamorphik
Copy link

👍👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment