shouldUseInlineStrings = $shouldUseInlineStrings; $this->fileSystemHelper = new FileSystemHelper($tempFolder); $this->fileSystemHelper->createBaseFilesAndFolders(); $this->styleHelper = new StyleHelper($defaultRowStyle); // This helper will be shared by all sheets $xlFolder = $this->fileSystemHelper->getXlFolder(); $this->sharedStringsHelper = new SharedStringsHelper($xlFolder); } /** * @return \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles to XLSX files */ protected function getStyleHelper() { return $this->styleHelper; } /** * @return int Maximum number of rows/columns a sheet can contain */ protected function getMaxRowsPerWorksheet() { return self::$maxRowsPerWorksheet; } /** * Creates a new sheet in the workbook. The current sheet remains unchanged. * * @return Worksheet The created sheet * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing */ public function addNewSheet() { $newSheetIndex = count($this->worksheets); $sheet = new Sheet($newSheetIndex); $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings); $this->worksheets[] = $worksheet; return $worksheet; } /** * Closes the workbook and all its associated sheets. * All the necessary files are written to disk and zipped together to create the XLSX file. * All the temporary files are then deleted. * * @param resource $finalFilePointer Pointer to the XLSX that will be created * @return void */ public function close($finalFilePointer) { /** @var Worksheet[] $worksheets */ $worksheets = $this->worksheets; foreach ($worksheets as $worksheet) { $worksheet->close(); } $this->sharedStringsHelper->close(); // Finish creating all the necessary files before zipping everything together $this->fileSystemHelper ->createContentTypesFile($worksheets) ->createWorkbookFile($worksheets) ->createWorkbookRelsFile($worksheets) ->createStylesFile($this->styleHelper) ->zipRootFolderAndCopyToStream($finalFilePointer); $this->cleanupTempFolder(); } /** * Deletes the root folder created in the temp folder and all its contents. * * @return void */ protected function cleanupTempFolder() { $xlsxRootFolder = $this->fileSystemHelper->getRootFolder(); $this->fileSystemHelper->deleteFolderRecursively($xlsxRootFolder); } }