sharedStringsFilePointer = fopen($sharedStringsFilePath, 'w'); $this->throwIfSharedStringsFilePointerIsNotAvailable(); // the headers is split into different parts so that we can fseek and put in the correct count and uniqueCount later $header = self::SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER . ' ' . self::DEFAULT_STRINGS_COUNT_PART . '>'; fwrite($this->sharedStringsFilePointer, $header); /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ $this->stringsEscaper = new \Box\Spout\Common\Escaper\XLSX(); } /** * Checks if the book has been created. Throws an exception if not created yet. * * @return void * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing */ protected function throwIfSharedStringsFilePointerIsNotAvailable() { if (!$this->sharedStringsFilePointer) { throw new IOException('Unable to open shared strings file for writing.'); } } /** * Writes the given string into the sharedStrings.xml file. * Starting and ending whitespaces are preserved. * * @param string $string * @return int ID of the written shared string */ public function writeString($string) { fwrite($this->sharedStringsFilePointer, '' . $this->stringsEscaper->escape($string) . ''); $this->numSharedStrings++; // Shared string ID is zero-based return ($this->numSharedStrings - 1); } /** * Finishes writing the data in the sharedStrings.xml file and closes the file. * * @return void */ public function close() { fwrite($this->sharedStringsFilePointer, ''); // Replace the default strings count with the actual number of shared strings in the file header $firstPartHeaderLength = strlen(self::SHARED_STRINGS_XML_FILE_FIRST_PART_HEADER); $defaultStringsCountPartLength = strlen(self::DEFAULT_STRINGS_COUNT_PART); // Adding 1 to take into account the space between the last xml attribute and "count" fseek($this->sharedStringsFilePointer, $firstPartHeaderLength + 1); fwrite($this->sharedStringsFilePointer, sprintf("%-{$defaultStringsCountPartLength}s", 'count="' . $this->numSharedStrings . '" uniqueCount="' . $this->numSharedStrings . '"')); fclose($this->sharedStringsFilePointer); } }