first commit
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Helper;
|
||||
|
||||
use Box\Spout\Writer\Style\BorderPart;
|
||||
use Box\Spout\Writer\Style\Border;
|
||||
|
||||
/**
|
||||
* Class BorderHelper
|
||||
*
|
||||
* The fo:border, fo:border-top, fo:border-bottom, fo:border-left and fo:border-right attributes
|
||||
* specify border properties
|
||||
* http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1419780_253892949
|
||||
*
|
||||
* Example table-cell-properties
|
||||
*
|
||||
* <style:table-cell-properties
|
||||
* fo:border-bottom="0.74pt solid #ffc000" style:diagonal-bl-tr="none"
|
||||
* style:diagonal-tl-br="none" fo:border-left="none" fo:border-right="none"
|
||||
* style:rotation-align="none" fo:border-top="none"/>
|
||||
*/
|
||||
class BorderHelper
|
||||
{
|
||||
/**
|
||||
* Width mappings
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $widthMap = [
|
||||
Border::WIDTH_THIN => '0.75pt',
|
||||
Border::WIDTH_MEDIUM => '1.75pt',
|
||||
Border::WIDTH_THICK => '2.5pt',
|
||||
];
|
||||
|
||||
/**
|
||||
* Style mapping
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $styleMap = [
|
||||
Border::STYLE_SOLID => 'solid',
|
||||
Border::STYLE_DASHED => 'dashed',
|
||||
Border::STYLE_DOTTED => 'dotted',
|
||||
Border::STYLE_DOUBLE => 'double',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param BorderPart $borderPart
|
||||
* @return string
|
||||
*/
|
||||
public static function serializeBorderPart(BorderPart $borderPart)
|
||||
{
|
||||
$definition = 'fo:border-%s="%s"';
|
||||
|
||||
if ($borderPart->getStyle() === Border::STYLE_NONE) {
|
||||
$borderPartDefinition = sprintf($definition, $borderPart->getName(), 'none');
|
||||
} else {
|
||||
$attributes = [
|
||||
self::$widthMap[$borderPart->getWidth()],
|
||||
self::$styleMap[$borderPart->getStyle()],
|
||||
'#' . $borderPart->getColor(),
|
||||
];
|
||||
$borderPartDefinition = sprintf($definition, $borderPart->getName(), implode(' ', $attributes));
|
||||
}
|
||||
|
||||
return $borderPartDefinition;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Helper;
|
||||
|
||||
use Box\Spout\Writer\Common\Helper\ZipHelper;
|
||||
use Box\Spout\Writer\ODS\Internal\Worksheet;
|
||||
|
||||
/**
|
||||
* Class FileSystemHelper
|
||||
* This class provides helper functions to help with the file system operations
|
||||
* like files/folders creation & deletion for ODS files
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS\Helper
|
||||
*/
|
||||
class FileSystemHelper extends \Box\Spout\Common\Helper\FileSystemHelper
|
||||
{
|
||||
const APP_NAME = 'Spout';
|
||||
const MIMETYPE = 'application/vnd.oasis.opendocument.spreadsheet';
|
||||
|
||||
const META_INF_FOLDER_NAME = 'META-INF';
|
||||
const SHEETS_CONTENT_TEMP_FOLDER_NAME = 'worksheets-temp';
|
||||
|
||||
const MANIFEST_XML_FILE_NAME = 'manifest.xml';
|
||||
const CONTENT_XML_FILE_NAME = 'content.xml';
|
||||
const META_XML_FILE_NAME = 'meta.xml';
|
||||
const MIMETYPE_FILE_NAME = 'mimetype';
|
||||
const STYLES_XML_FILE_NAME = 'styles.xml';
|
||||
|
||||
/** @var string Path to the root folder inside the temp folder where the files to create the ODS will be stored */
|
||||
protected $rootFolder;
|
||||
|
||||
/** @var string Path to the "META-INF" folder inside the root folder */
|
||||
protected $metaInfFolder;
|
||||
|
||||
/** @var string Path to the temp folder, inside the root folder, where specific sheets content will be written to */
|
||||
protected $sheetsContentTempFolder;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRootFolder()
|
||||
{
|
||||
return $this->rootFolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSheetsContentTempFolder()
|
||||
{
|
||||
return $this->sheetsContentTempFolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates all the folders needed to create a ODS file, as well as the files that won't change.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders
|
||||
*/
|
||||
public function createBaseFilesAndFolders()
|
||||
{
|
||||
$this
|
||||
->createRootFolder()
|
||||
->createMetaInfoFolderAndFile()
|
||||
->createSheetsContentTempFolder()
|
||||
->createMetaFile()
|
||||
->createMimetypeFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the folder that will be used as root
|
||||
*
|
||||
* @return FileSystemHelper
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create the folder
|
||||
*/
|
||||
protected function createRootFolder()
|
||||
{
|
||||
$this->rootFolder = $this->createFolder($this->baseFolderRealPath, uniqid('ods'));
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the "META-INF" folder under the root folder as well as the "manifest.xml" file in it
|
||||
*
|
||||
* @return FileSystemHelper
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create the folder or the "manifest.xml" file
|
||||
*/
|
||||
protected function createMetaInfoFolderAndFile()
|
||||
{
|
||||
$this->metaInfFolder = $this->createFolder($this->rootFolder, self::META_INF_FOLDER_NAME);
|
||||
|
||||
$this->createManifestFile();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the "manifest.xml" file under the "META-INF" folder (under root)
|
||||
*
|
||||
* @return FileSystemHelper
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create the file
|
||||
*/
|
||||
protected function createManifestFile()
|
||||
{
|
||||
$manifestXmlFileContents = <<<EOD
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2">
|
||||
<manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/>
|
||||
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/>
|
||||
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/>
|
||||
<manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/>
|
||||
</manifest:manifest>
|
||||
EOD;
|
||||
|
||||
$this->createFileWithContents($this->metaInfFolder, self::MANIFEST_XML_FILE_NAME, $manifestXmlFileContents);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the temp folder where specific sheets content will be written to.
|
||||
* This folder is not part of the final ODS file and is only used to be able to jump between sheets.
|
||||
*
|
||||
* @return FileSystemHelper
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create the folder
|
||||
*/
|
||||
protected function createSheetsContentTempFolder()
|
||||
{
|
||||
$this->sheetsContentTempFolder = $this->createFolder($this->rootFolder, self::SHEETS_CONTENT_TEMP_FOLDER_NAME);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the "meta.xml" file under the root folder
|
||||
*
|
||||
* @return FileSystemHelper
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create the file
|
||||
*/
|
||||
protected function createMetaFile()
|
||||
{
|
||||
$appName = self::APP_NAME;
|
||||
$createdDate = (new \DateTime())->format(\DateTime::W3C);
|
||||
|
||||
$metaXmlFileContents = <<<EOD
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<office:document-meta office:version="1.2" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<office:meta>
|
||||
<dc:creator>$appName</dc:creator>
|
||||
<meta:creation-date>$createdDate</meta:creation-date>
|
||||
<dc:date>$createdDate</dc:date>
|
||||
</office:meta>
|
||||
</office:document-meta>
|
||||
EOD;
|
||||
|
||||
$this->createFileWithContents($this->rootFolder, self::META_XML_FILE_NAME, $metaXmlFileContents);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the "mimetype" file under the root folder
|
||||
*
|
||||
* @return FileSystemHelper
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create the file
|
||||
*/
|
||||
protected function createMimetypeFile()
|
||||
{
|
||||
$this->createFileWithContents($this->rootFolder, self::MIMETYPE_FILE_NAME, self::MIMETYPE);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the "content.xml" file under the root folder
|
||||
*
|
||||
* @param Worksheet[] $worksheets
|
||||
* @param StyleHelper $styleHelper
|
||||
* @return FileSystemHelper
|
||||
*/
|
||||
public function createContentFile($worksheets, $styleHelper)
|
||||
{
|
||||
$contentXmlFileContents = <<<EOD
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<office:document-content office:version="1.2" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:calcext="urn:org:documentfoundation:names:experimental:calc:xmlns:calcext:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:msoxl="http://schemas.microsoft.com/office/excel/formula" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
EOD;
|
||||
|
||||
$contentXmlFileContents .= $styleHelper->getContentXmlFontFaceSectionContent();
|
||||
$contentXmlFileContents .= $styleHelper->getContentXmlAutomaticStylesSectionContent(count($worksheets));
|
||||
|
||||
$contentXmlFileContents .= '<office:body><office:spreadsheet>';
|
||||
|
||||
$this->createFileWithContents($this->rootFolder, self::CONTENT_XML_FILE_NAME, $contentXmlFileContents);
|
||||
|
||||
// Append sheets content to "content.xml"
|
||||
$contentXmlFilePath = $this->rootFolder . '/' . self::CONTENT_XML_FILE_NAME;
|
||||
$contentXmlHandle = fopen($contentXmlFilePath, 'a');
|
||||
|
||||
foreach ($worksheets as $worksheet) {
|
||||
// write the "<table:table>" node, with the final sheet's name
|
||||
fwrite($contentXmlHandle, $worksheet->getTableElementStartAsString());
|
||||
|
||||
$worksheetFilePath = $worksheet->getWorksheetFilePath();
|
||||
$this->copyFileContentsToTarget($worksheetFilePath, $contentXmlHandle);
|
||||
|
||||
fwrite($contentXmlHandle, '</table:table>');
|
||||
}
|
||||
|
||||
$contentXmlFileContents = '</office:spreadsheet></office:body></office:document-content>';
|
||||
|
||||
fwrite($contentXmlHandle, $contentXmlFileContents);
|
||||
fclose($contentXmlHandle);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Streams the content of the file at the given path into the target resource.
|
||||
* Depending on which mode the target resource was created with, it will truncate then copy
|
||||
* or append the content to the target file.
|
||||
*
|
||||
* @param string $sourceFilePath Path of the file whose content will be copied
|
||||
* @param resource $targetResource Target resource that will receive the content
|
||||
* @return void
|
||||
*/
|
||||
protected function copyFileContentsToTarget($sourceFilePath, $targetResource)
|
||||
{
|
||||
$sourceHandle = fopen($sourceFilePath, 'r');
|
||||
stream_copy_to_stream($sourceHandle, $targetResource);
|
||||
fclose($sourceHandle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the temporary folder where sheets content was stored.
|
||||
*
|
||||
* @return FileSystemHelper
|
||||
*/
|
||||
public function deleteWorksheetTempFolder()
|
||||
{
|
||||
$this->deleteFolderRecursively($this->sheetsContentTempFolder);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates the "styles.xml" file under the root folder
|
||||
*
|
||||
* @param StyleHelper $styleHelper
|
||||
* @param int $numWorksheets Number of created worksheets
|
||||
* @return FileSystemHelper
|
||||
*/
|
||||
public function createStylesFile($styleHelper, $numWorksheets)
|
||||
{
|
||||
$stylesXmlFileContents = $styleHelper->getStylesXMLFileContent($numWorksheets);
|
||||
$this->createFileWithContents($this->rootFolder, self::STYLES_XML_FILE_NAME, $stylesXmlFileContents);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zips the root folder and streams the contents of the zip into the given stream
|
||||
*
|
||||
* @param resource $streamPointer Pointer to the stream to copy the zip
|
||||
* @return void
|
||||
*/
|
||||
public function zipRootFolderAndCopyToStream($streamPointer)
|
||||
{
|
||||
$zipHelper = new ZipHelper($this->rootFolder);
|
||||
|
||||
// In order to have the file's mime type detected properly, files need to be added
|
||||
// to the zip file in a particular order.
|
||||
// @see http://www.jejik.com/articles/2010/03/how_to_correctly_create_odf_documents_using_zip/
|
||||
$zipHelper->addUncompressedFileToArchive($this->rootFolder, self::MIMETYPE_FILE_NAME);
|
||||
|
||||
$zipHelper->addFolderToArchive($this->rootFolder, ZipHelper::EXISTING_FILES_SKIP);
|
||||
$zipHelper->closeArchiveAndCopyToStream($streamPointer);
|
||||
|
||||
// once the zip is copied, remove it
|
||||
$this->deleteFile($zipHelper->getZipFilePath());
|
||||
}
|
||||
}
|
||||
356
modules/x13import/tools/Spout/Writer/ODS/Helper/StyleHelper.php
Normal file
356
modules/x13import/tools/Spout/Writer/ODS/Helper/StyleHelper.php
Normal file
@@ -0,0 +1,356 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Helper;
|
||||
|
||||
use Box\Spout\Writer\Common\Helper\AbstractStyleHelper;
|
||||
use Box\Spout\Writer\Style\BorderPart;
|
||||
|
||||
/**
|
||||
* Class StyleHelper
|
||||
* This class provides helper functions to manage styles
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS\Helper
|
||||
*/
|
||||
class StyleHelper extends AbstractStyleHelper
|
||||
{
|
||||
/** @var string[] [FONT_NAME] => [] Map whose keys contain all the fonts used */
|
||||
protected $usedFontsSet = [];
|
||||
|
||||
/**
|
||||
* Registers the given style as a used style.
|
||||
* Duplicate styles won't be registered more than once.
|
||||
*
|
||||
* @param \Box\Spout\Writer\Style\Style $style The style to be registered
|
||||
* @return \Box\Spout\Writer\Style\Style The registered style, updated with an internal ID.
|
||||
*/
|
||||
public function registerStyle($style)
|
||||
{
|
||||
$this->usedFontsSet[$style->getFontName()] = true;
|
||||
return parent::registerStyle($style);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[] List of used fonts name
|
||||
*/
|
||||
protected function getUsedFonts()
|
||||
{
|
||||
return array_keys($this->usedFontsSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "styles.xml" file, given a list of styles.
|
||||
*
|
||||
* @param int $numWorksheets Number of worksheets created
|
||||
* @return string
|
||||
*/
|
||||
public function getStylesXMLFileContent($numWorksheets)
|
||||
{
|
||||
$content = <<<EOD
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<office:document-styles office:version="1.2" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:msoxl="http://schemas.microsoft.com/office/excel/formula" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
EOD;
|
||||
|
||||
$content .= $this->getFontFaceSectionContent();
|
||||
$content .= $this->getStylesSectionContent();
|
||||
$content .= $this->getAutomaticStylesSectionContent($numWorksheets);
|
||||
$content .= $this->getMasterStylesSectionContent($numWorksheets);
|
||||
|
||||
$content .= <<<EOD
|
||||
</office:document-styles>
|
||||
EOD;
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "<office:font-face-decls>" section, inside "styles.xml" file.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getFontFaceSectionContent()
|
||||
{
|
||||
$content = '<office:font-face-decls>';
|
||||
foreach ($this->getUsedFonts() as $fontName) {
|
||||
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
|
||||
}
|
||||
$content .= '</office:font-face-decls>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "<office:styles>" section, inside "styles.xml" file.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getStylesSectionContent()
|
||||
{
|
||||
$defaultStyle = $this->getDefaultStyle();
|
||||
|
||||
return <<<EOD
|
||||
<office:styles>
|
||||
<number:number-style style:name="N0">
|
||||
<number:number number:min-integer-digits="1"/>
|
||||
</number:number-style>
|
||||
<style:style style:data-style-name="N0" style:family="table-cell" style:name="Default">
|
||||
<style:table-cell-properties fo:background-color="transparent" style:vertical-align="automatic"/>
|
||||
<style:text-properties fo:color="#{$defaultStyle->getFontColor()}"
|
||||
fo:font-size="{$defaultStyle->getFontSize()}pt" style:font-size-asian="{$defaultStyle->getFontSize()}pt" style:font-size-complex="{$defaultStyle->getFontSize()}pt"
|
||||
style:font-name="{$defaultStyle->getFontName()}" style:font-name-asian="{$defaultStyle->getFontName()}" style:font-name-complex="{$defaultStyle->getFontName()}"/>
|
||||
</style:style>
|
||||
</office:styles>
|
||||
EOD;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "<office:automatic-styles>" section, inside "styles.xml" file.
|
||||
*
|
||||
* @param int $numWorksheets Number of worksheets created
|
||||
* @return string
|
||||
*/
|
||||
protected function getAutomaticStylesSectionContent($numWorksheets)
|
||||
{
|
||||
$content = '<office:automatic-styles>';
|
||||
|
||||
for ($i = 1; $i <= $numWorksheets; $i++) {
|
||||
$content .= <<<EOD
|
||||
<style:page-layout style:name="pm$i">
|
||||
<style:page-layout-properties style:first-page-number="continue" style:print="objects charts drawings" style:table-centering="none"/>
|
||||
<style:header-style/>
|
||||
<style:footer-style/>
|
||||
</style:page-layout>
|
||||
EOD;
|
||||
}
|
||||
|
||||
$content .= '</office:automatic-styles>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "<office:master-styles>" section, inside "styles.xml" file.
|
||||
*
|
||||
* @param int $numWorksheets Number of worksheets created
|
||||
* @return string
|
||||
*/
|
||||
protected function getMasterStylesSectionContent($numWorksheets)
|
||||
{
|
||||
$content = '<office:master-styles>';
|
||||
|
||||
for ($i = 1; $i <= $numWorksheets; $i++) {
|
||||
$content .= <<<EOD
|
||||
<style:master-page style:name="mp$i" style:page-layout-name="pm$i">
|
||||
<style:header/>
|
||||
<style:header-left style:display="false"/>
|
||||
<style:footer/>
|
||||
<style:footer-left style:display="false"/>
|
||||
</style:master-page>
|
||||
EOD;
|
||||
}
|
||||
|
||||
$content .= '</office:master-styles>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the contents of the "<office:font-face-decls>" section, inside "content.xml" file.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContentXmlFontFaceSectionContent()
|
||||
{
|
||||
$content = '<office:font-face-decls>';
|
||||
foreach ($this->getUsedFonts() as $fontName) {
|
||||
$content .= '<style:font-face style:name="' . $fontName . '" svg:font-family="' . $fontName . '"/>';
|
||||
}
|
||||
$content .= '</office:font-face-decls>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the "<office:automatic-styles>" section, inside "content.xml" file.
|
||||
*
|
||||
* @param int $numWorksheets Number of worksheets created
|
||||
* @return string
|
||||
*/
|
||||
public function getContentXmlAutomaticStylesSectionContent($numWorksheets)
|
||||
{
|
||||
$content = '<office:automatic-styles>';
|
||||
|
||||
foreach ($this->getRegisteredStyles() as $style) {
|
||||
$content .= $this->getStyleSectionContent($style);
|
||||
}
|
||||
|
||||
$content .= <<<EOD
|
||||
<style:style style:family="table-column" style:name="co1">
|
||||
<style:table-column-properties fo:break-before="auto"/>
|
||||
</style:style>
|
||||
<style:style style:family="table-row" style:name="ro1">
|
||||
<style:table-row-properties fo:break-before="auto" style:row-height="15pt" style:use-optimal-row-height="true"/>
|
||||
</style:style>
|
||||
EOD;
|
||||
|
||||
for ($i = 1; $i <= $numWorksheets; $i++) {
|
||||
$content .= <<<EOD
|
||||
<style:style style:family="table" style:master-page-name="mp$i" style:name="ta$i">
|
||||
<style:table-properties style:writing-mode="lr-tb" table:display="true"/>
|
||||
</style:style>
|
||||
EOD;
|
||||
}
|
||||
|
||||
$content .= '</office:automatic-styles>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section
|
||||
*
|
||||
* @param \Box\Spout\Writer\Style\Style $style
|
||||
* @return string
|
||||
*/
|
||||
protected function getStyleSectionContent($style)
|
||||
{
|
||||
$styleIndex = $style->getId() + 1; // 1-based
|
||||
|
||||
$content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">';
|
||||
|
||||
$content .= $this->getTextPropertiesSectionContent($style);
|
||||
$content .= $this->getTableCellPropertiesSectionContent($style);
|
||||
|
||||
$content .= '</style:style>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
|
||||
*
|
||||
* @param \Box\Spout\Writer\Style\Style $style
|
||||
* @return string
|
||||
*/
|
||||
private function getTextPropertiesSectionContent($style)
|
||||
{
|
||||
$content = '';
|
||||
|
||||
if ($style->shouldApplyFont()) {
|
||||
$content .= $this->getFontSectionContent($style);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the "<style:text-properties>" section, inside "<style:style>" section
|
||||
*
|
||||
* @param \Box\Spout\Writer\Style\Style $style
|
||||
* @return string
|
||||
*/
|
||||
private function getFontSectionContent($style)
|
||||
{
|
||||
$defaultStyle = $this->getDefaultStyle();
|
||||
|
||||
$content = '<style:text-properties';
|
||||
|
||||
$fontColor = $style->getFontColor();
|
||||
if ($fontColor !== $defaultStyle->getFontColor()) {
|
||||
$content .= ' fo:color="#' . $fontColor . '"';
|
||||
}
|
||||
|
||||
$fontName = $style->getFontName();
|
||||
if ($fontName !== $defaultStyle->getFontName()) {
|
||||
$content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"';
|
||||
}
|
||||
|
||||
$fontSize = $style->getFontSize();
|
||||
if ($fontSize !== $defaultStyle->getFontSize()) {
|
||||
$content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"';
|
||||
}
|
||||
|
||||
if ($style->isFontBold()) {
|
||||
$content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"';
|
||||
}
|
||||
if ($style->isFontItalic()) {
|
||||
$content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"';
|
||||
}
|
||||
if ($style->isFontUnderline()) {
|
||||
$content .= ' style:text-underline-style="solid" style:text-underline-type="single"';
|
||||
}
|
||||
if ($style->isFontStrikethrough()) {
|
||||
$content .= ' style:text-line-through-style="solid"';
|
||||
}
|
||||
|
||||
$content .= '/>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section
|
||||
*
|
||||
* @param \Box\Spout\Writer\Style\Style $style
|
||||
* @return string
|
||||
*/
|
||||
private function getTableCellPropertiesSectionContent($style)
|
||||
{
|
||||
$content = '';
|
||||
|
||||
if ($style->shouldWrapText()) {
|
||||
$content .= $this->getWrapTextXMLContent();
|
||||
}
|
||||
|
||||
if ($style->shouldApplyBorder()) {
|
||||
$content .= $this->getBorderXMLContent($style);
|
||||
}
|
||||
|
||||
if ($style->shouldApplyBackgroundColor()) {
|
||||
$content .= $this->getBackgroundColorXMLContent($style);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the wrap text definition for the "<style:table-cell-properties>" section
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getWrapTextXMLContent()
|
||||
{
|
||||
return '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the borders definition for the "<style:table-cell-properties>" section
|
||||
*
|
||||
* @param \Box\Spout\Writer\Style\Style $style
|
||||
* @return string
|
||||
*/
|
||||
private function getBorderXMLContent($style)
|
||||
{
|
||||
$borderProperty = '<style:table-cell-properties %s />';
|
||||
|
||||
$borders = array_map(function (BorderPart $borderPart) {
|
||||
return BorderHelper::serializeBorderPart($borderPart);
|
||||
}, $style->getBorder()->getParts());
|
||||
|
||||
return sprintf($borderProperty, implode(' ', $borders));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the contents of the background color definition for the "<style:table-cell-properties>" section
|
||||
*
|
||||
* @param \Box\Spout\Writer\Style\Style $style
|
||||
* @return string
|
||||
*/
|
||||
private function getBackgroundColorXMLContent($style)
|
||||
{
|
||||
return sprintf(
|
||||
'<style:table-cell-properties fo:background-color="#%s"/>',
|
||||
$style->getBackgroundColor()
|
||||
);
|
||||
}
|
||||
}
|
||||
119
modules/x13import/tools/Spout/Writer/ODS/Internal/Workbook.php
Normal file
119
modules/x13import/tools/Spout/Writer/ODS/Internal/Workbook.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Internal;
|
||||
|
||||
use Box\Spout\Writer\Common\Internal\AbstractWorkbook;
|
||||
use Box\Spout\Writer\ODS\Helper\FileSystemHelper;
|
||||
use Box\Spout\Writer\ODS\Helper\StyleHelper;
|
||||
use Box\Spout\Writer\Common\Sheet;
|
||||
|
||||
/**
|
||||
* Class Workbook
|
||||
* Represents a workbook within a ODS file.
|
||||
* It provides the functions to work with worksheets.
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS\Internal
|
||||
*/
|
||||
class Workbook extends AbstractWorkbook
|
||||
{
|
||||
/**
|
||||
* Maximum number of rows a ODS sheet can contain
|
||||
* @see https://ask.libreoffice.org/en/question/8631/upper-limit-to-number-of-rows-in-calc/
|
||||
*/
|
||||
protected static $maxRowsPerWorksheet = 1048576;
|
||||
|
||||
/** @var \Box\Spout\Writer\ODS\Helper\FileSystemHelper Helper to perform file system operations */
|
||||
protected $fileSystemHelper;
|
||||
|
||||
/** @var \Box\Spout\Writer\ODS\Helper\StyleHelper Helper to apply styles */
|
||||
protected $styleHelper;
|
||||
|
||||
/**
|
||||
* @param string $tempFolder
|
||||
* @param bool $shouldCreateNewSheetsAutomatically
|
||||
* @param \Box\Spout\Writer\Style\Style $defaultRowStyle
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders
|
||||
*/
|
||||
public function __construct($tempFolder, $shouldCreateNewSheetsAutomatically, $defaultRowStyle)
|
||||
{
|
||||
parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle);
|
||||
|
||||
$this->fileSystemHelper = new FileSystemHelper($tempFolder);
|
||||
$this->fileSystemHelper->createBaseFilesAndFolders();
|
||||
|
||||
$this->styleHelper = new StyleHelper($defaultRowStyle);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Box\Spout\Writer\ODS\Helper\StyleHelper Helper to apply styles to ODS 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, $this->internalId);
|
||||
|
||||
$sheetsContentTempFolder = $this->fileSystemHelper->getSheetsContentTempFolder();
|
||||
$worksheet = new Worksheet($sheet, $sheetsContentTempFolder);
|
||||
$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 ODS file.
|
||||
* All the temporary files are then deleted.
|
||||
*
|
||||
* @param resource $finalFilePointer Pointer to the ODS that will be created
|
||||
* @return void
|
||||
*/
|
||||
public function close($finalFilePointer)
|
||||
{
|
||||
/** @var Worksheet[] $worksheets */
|
||||
$worksheets = $this->worksheets;
|
||||
$numWorksheets = count($worksheets);
|
||||
|
||||
foreach ($worksheets as $worksheet) {
|
||||
$worksheet->close();
|
||||
}
|
||||
|
||||
// Finish creating all the necessary files before zipping everything together
|
||||
$this->fileSystemHelper
|
||||
->createContentFile($worksheets, $this->styleHelper)
|
||||
->deleteWorksheetTempFolder()
|
||||
->createStylesFile($this->styleHelper, $numWorksheets)
|
||||
->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);
|
||||
}
|
||||
}
|
||||
233
modules/x13import/tools/Spout/Writer/ODS/Internal/Worksheet.php
Normal file
233
modules/x13import/tools/Spout/Writer/ODS/Internal/Worksheet.php
Normal file
@@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS\Internal;
|
||||
|
||||
use Box\Spout\Common\Exception\InvalidArgumentException;
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Helper\StringHelper;
|
||||
use Box\Spout\Writer\Common\Helper\CellHelper;
|
||||
use Box\Spout\Writer\Common\Internal\WorksheetInterface;
|
||||
|
||||
/**
|
||||
* Class Worksheet
|
||||
* Represents a worksheet within a ODS file. The difference with the Sheet object is
|
||||
* that this class provides an interface to write data
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS\Internal
|
||||
*/
|
||||
class Worksheet implements WorksheetInterface
|
||||
{
|
||||
/** @var \Box\Spout\Writer\Common\Sheet The "external" sheet */
|
||||
protected $externalSheet;
|
||||
|
||||
/** @var string Path to the XML file that will contain the sheet data */
|
||||
protected $worksheetFilePath;
|
||||
|
||||
/** @var \Box\Spout\Common\Escaper\ODS Strings escaper */
|
||||
protected $stringsEscaper;
|
||||
|
||||
/** @var \Box\Spout\Common\Helper\StringHelper To help with string manipulation */
|
||||
protected $stringHelper;
|
||||
|
||||
/** @var Resource Pointer to the temporary sheet data file (e.g. worksheets-temp/sheet1.xml) */
|
||||
protected $sheetFilePointer;
|
||||
|
||||
/** @var int Maximum number of columns among all the written rows */
|
||||
protected $maxNumColumns = 1;
|
||||
|
||||
/** @var int Index of the last written row */
|
||||
protected $lastWrittenRowIndex = 0;
|
||||
|
||||
/**
|
||||
* @param \Box\Spout\Writer\Common\Sheet $externalSheet The associated "external" sheet
|
||||
* @param string $worksheetFilesFolder Temporary folder where the files to create the ODS will be stored
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing
|
||||
*/
|
||||
public function __construct($externalSheet, $worksheetFilesFolder)
|
||||
{
|
||||
$this->externalSheet = $externalSheet;
|
||||
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
|
||||
$this->stringsEscaper = \Box\Spout\Common\Escaper\ODS::getInstance();
|
||||
$this->worksheetFilePath = $worksheetFilesFolder . '/sheet' . $externalSheet->getIndex() . '.xml';
|
||||
|
||||
$this->stringHelper = new StringHelper();
|
||||
|
||||
$this->startSheet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the worksheet to accept data
|
||||
* The XML file does not contain the "<table:table>" node as it contains the sheet's name
|
||||
* which may change during the execution of the program. It will be added at the end.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing
|
||||
*/
|
||||
protected function startSheet()
|
||||
{
|
||||
$this->sheetFilePointer = fopen($this->worksheetFilePath, 'w');
|
||||
$this->throwIfSheetFilePointerIsNotAvailable();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 throwIfSheetFilePointerIsNotAvailable()
|
||||
{
|
||||
if (!$this->sheetFilePointer) {
|
||||
throw new IOException('Unable to open sheet for writing.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string Path to the temporary sheet content XML file
|
||||
*/
|
||||
public function getWorksheetFilePath()
|
||||
{
|
||||
return $this->worksheetFilePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the table XML root node as string.
|
||||
*
|
||||
* @return string <table> node as string
|
||||
*/
|
||||
public function getTableElementStartAsString()
|
||||
{
|
||||
$escapedSheetName = $this->stringsEscaper->escape($this->externalSheet->getName());
|
||||
$tableStyleName = 'ta' . ($this->externalSheet->getIndex() + 1);
|
||||
|
||||
$tableElement = '<table:table table:style-name="' . $tableStyleName . '" table:name="' . $escapedSheetName . '">';
|
||||
$tableElement .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="co1" table:number-columns-repeated="' . $this->maxNumColumns . '"/>';
|
||||
|
||||
return $tableElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Box\Spout\Writer\Common\Sheet The "external" sheet
|
||||
*/
|
||||
public function getExternalSheet()
|
||||
{
|
||||
return $this->externalSheet;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int The index of the last written row
|
||||
*/
|
||||
public function getLastWrittenRowIndex()
|
||||
{
|
||||
return $this->lastWrittenRowIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds data to the worksheet.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be written. Cannot be empty.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style.
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If the data cannot be written
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
|
||||
*/
|
||||
public function addRow($dataRow, $style)
|
||||
{
|
||||
// $dataRow can be an associative array. We need to transform
|
||||
// it into a regular array, as we'll use the numeric indexes.
|
||||
$dataRowWithNumericIndexes = array_values($dataRow);
|
||||
|
||||
$styleIndex = ($style->getId() + 1); // 1-based
|
||||
$cellsCount = count($dataRow);
|
||||
$this->maxNumColumns = max($this->maxNumColumns, $cellsCount);
|
||||
|
||||
$data = '<table:table-row table:style-name="ro1">';
|
||||
|
||||
$currentCellIndex = 0;
|
||||
$nextCellIndex = 1;
|
||||
|
||||
for ($i = 0; $i < $cellsCount; $i++) {
|
||||
$currentCellValue = $dataRowWithNumericIndexes[$currentCellIndex];
|
||||
|
||||
// Using isset here because it is way faster than array_key_exists...
|
||||
if (!isset($dataRowWithNumericIndexes[$nextCellIndex]) ||
|
||||
$currentCellValue !== $dataRowWithNumericIndexes[$nextCellIndex]) {
|
||||
|
||||
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex);
|
||||
$data .= $this->getCellXML($currentCellValue, $styleIndex, $numTimesValueRepeated);
|
||||
|
||||
$currentCellIndex = $nextCellIndex;
|
||||
}
|
||||
|
||||
$nextCellIndex++;
|
||||
}
|
||||
|
||||
$data .= '</table:table-row>';
|
||||
|
||||
$wasWriteSuccessful = fwrite($this->sheetFilePointer, $data);
|
||||
if ($wasWriteSuccessful === false) {
|
||||
throw new IOException("Unable to write data in {$this->worksheetFilePath}");
|
||||
}
|
||||
|
||||
// only update the count if the write worked
|
||||
$this->lastWrittenRowIndex++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cell XML content, given its value.
|
||||
*
|
||||
* @param mixed $cellValue The value to be written
|
||||
* @param int $styleIndex Index of the used style
|
||||
* @param int $numTimesValueRepeated Number of times the value is consecutively repeated
|
||||
* @return string The cell XML content
|
||||
* @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported
|
||||
*/
|
||||
protected function getCellXML($cellValue, $styleIndex, $numTimesValueRepeated)
|
||||
{
|
||||
$data = '<table:table-cell table:style-name="ce' . $styleIndex . '"';
|
||||
|
||||
if ($numTimesValueRepeated !== 1) {
|
||||
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"';
|
||||
}
|
||||
|
||||
if (CellHelper::isNonEmptyString($cellValue)) {
|
||||
$data .= ' office:value-type="string" calcext:value-type="string">';
|
||||
|
||||
$cellValueLines = explode("\n", $cellValue);
|
||||
foreach ($cellValueLines as $cellValueLine) {
|
||||
$data .= '<text:p>' . $this->stringsEscaper->escape($cellValueLine) . '</text:p>';
|
||||
}
|
||||
|
||||
$data .= '</table:table-cell>';
|
||||
} else if (CellHelper::isBoolean($cellValue)) {
|
||||
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellValue . '">';
|
||||
$data .= '<text:p>' . $cellValue . '</text:p>';
|
||||
$data .= '</table:table-cell>';
|
||||
} else if (CellHelper::isNumeric($cellValue)) {
|
||||
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellValue . '">';
|
||||
$data .= '<text:p>' . $cellValue . '</text:p>';
|
||||
$data .= '</table:table-cell>';
|
||||
} else if (empty($cellValue)) {
|
||||
$data .= '/>';
|
||||
} else {
|
||||
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue));
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the worksheet
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if (!is_resource($this->sheetFilePointer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
fclose($this->sheetFilePointer);
|
||||
}
|
||||
}
|
||||
93
modules/x13import/tools/Spout/Writer/ODS/Writer.php
Normal file
93
modules/x13import/tools/Spout/Writer/ODS/Writer.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace Box\Spout\Writer\ODS;
|
||||
|
||||
use Box\Spout\Writer\AbstractMultiSheetsWriter;
|
||||
use Box\Spout\Writer\Common;
|
||||
use Box\Spout\Writer\ODS\Internal\Workbook;
|
||||
|
||||
/**
|
||||
* Class Writer
|
||||
* This class provides base support to write data to ODS files
|
||||
*
|
||||
* @package Box\Spout\Writer\ODS
|
||||
*/
|
||||
class Writer extends AbstractMultiSheetsWriter
|
||||
{
|
||||
/** @var string Content-Type value for the header */
|
||||
protected static $headerContentType = 'application/vnd.oasis.opendocument.spreadsheet';
|
||||
|
||||
/** @var string Temporary folder where the files to create the ODS will be stored */
|
||||
protected $tempFolder;
|
||||
|
||||
/** @var Internal\Workbook The workbook for the ODS file */
|
||||
protected $book;
|
||||
|
||||
/**
|
||||
* Sets a custom temporary folder for creating intermediate files/folders.
|
||||
* This must be set before opening the writer.
|
||||
*
|
||||
* @api
|
||||
* @param string $tempFolder Temporary folder where the files to create the ODS will be stored
|
||||
* @return Writer
|
||||
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
|
||||
*/
|
||||
public function setTempFolder($tempFolder)
|
||||
{
|
||||
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
|
||||
|
||||
$this->tempFolder = $tempFolder;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the write and sets the current sheet pointer to a new sheet.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to open the file for writing
|
||||
*/
|
||||
protected function openWriter()
|
||||
{
|
||||
$tempFolder = ($this->tempFolder) ? : sys_get_temp_dir();
|
||||
$this->book = new Workbook($tempFolder, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle);
|
||||
$this->book->addNewSheetAndMakeItCurrent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Internal\Workbook The workbook representing the file to be written
|
||||
*/
|
||||
protected function getWorkbook()
|
||||
{
|
||||
return $this->book;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds data to the currently opened writer.
|
||||
* If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
|
||||
* with the creation of new worksheets if one worksheet has reached its maximum capicity.
|
||||
*
|
||||
* @param array $dataRow Array containing data to be written.
|
||||
* Example $dataRow = ['data1', 1234, null, '', 'data5'];
|
||||
* @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
|
||||
* @return void
|
||||
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet
|
||||
* @throws \Box\Spout\Common\Exception\IOException If unable to write data
|
||||
*/
|
||||
protected function addRowToWriter(array $dataRow, $style)
|
||||
{
|
||||
$this->throwIfBookIsNotAvailable();
|
||||
$this->book->addRowToCurrentWorksheet($dataRow, $style);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the writer, preventing any additional writing.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function closeWriter()
|
||||
{
|
||||
if ($this->book) {
|
||||
$this->book->close($this->filePointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user