From 4b6bf0c2efe92dab47db69a39d3a6d75c55cc011 Mon Sep 17 00:00:00 2001 From: abo Date: Thu, 14 Sep 2017 16:44:38 +0300 Subject: [PATCH] Add ability to set column widths. --- src/Spout/Writer/XLSX/Internal/Workbook.php | 21 ++++++++- src/Spout/Writer/XLSX/Internal/Worksheet.php | 22 ++++++++-- src/Spout/Writer/XLSX/Writer.php | 45 +++++++++++++++++++- 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src/Spout/Writer/XLSX/Internal/Workbook.php b/src/Spout/Writer/XLSX/Internal/Workbook.php index bcdce7f..a9c4301 100644 --- a/src/Spout/Writer/XLSX/Internal/Workbook.php +++ b/src/Spout/Writer/XLSX/Internal/Workbook.php @@ -35,14 +35,18 @@ class Workbook extends AbstractWorkbook /** @var \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles */ protected $styleHelper; + /** @var array contain column width information */ + protected $columnWidths = []; + /** * @param string $tempFolder * @param bool $shouldUseInlineStrings * @param bool $shouldCreateNewSheetsAutomatically * @param \Box\Spout\Writer\Style\Style $defaultRowStyle + * @param array $columnWidths * @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders */ - public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle) + public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle, $columnWidths) { parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle); @@ -56,6 +60,8 @@ class Workbook extends AbstractWorkbook // This helper will be shared by all sheets $xlFolder = $this->fileSystemHelper->getXlFolder(); $this->sharedStringsHelper = new SharedStringsHelper($xlFolder); + + $this->columnWidths = $columnWidths; } /** @@ -86,12 +92,23 @@ class Workbook extends AbstractWorkbook $sheet = new Sheet($newSheetIndex, $this->internalId); $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); - $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->shouldUseInlineStrings); + $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->styleHelper, $this->shouldUseInlineStrings, $this->columnWidths); $this->worksheets[] = $worksheet; return $worksheet; } + /** + * Set column width for sheet that will be created + * should only be called from the writer + * + * @param array $columnWidths + */ + public function _setColumnWidths($columnWidths) + { + $this->columnWidths = $columnWidths; + } + /** * Closes the workbook and all its associated sheets. * All the necessary files are written to disk and zipped together to create the XLSX file. diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index b5a3dc7..a873031 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -63,9 +63,10 @@ EOD; * @param \Box\Spout\Writer\XLSX\Helper\SharedStringsHelper $sharedStringsHelper Helper for shared strings * @param \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to work with styles * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used + * @param array $columnWidths array of column widths * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing */ - public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $styleHelper, $shouldUseInlineStrings) + public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $styleHelper, $shouldUseInlineStrings, $columnWidths) { $this->externalSheet = $externalSheet; $this->sharedStringsHelper = $sharedStringsHelper; @@ -77,21 +78,36 @@ EOD; $this->stringHelper = new StringHelper(); $this->worksheetFilePath = $worksheetFilesFolder . '/' . strtolower($this->externalSheet->getName()) . '.xml'; - $this->startSheet(); + $this->startSheet($columnWidths); } /** * Prepares the worksheet to accept data * + * @param array $columnWidths * @return void * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing */ - protected function startSheet() + protected function startSheet($columnWidths) { $this->sheetFilePointer = fopen($this->worksheetFilePath, 'w'); $this->throwIfSheetFilePointerIsNotAvailable(); fwrite($this->sheetFilePointer, self::SHEET_XML_FILE_HEADER); + + if (!empty($columnWidths)) { + fwrite($this->sheetFilePointer, ''); + foreach ($columnWidths as $columnWidth) { + fwrite($this->sheetFilePointer, + '' + ); + } + fwrite($this->sheetFilePointer, ''); + } + fwrite($this->sheetFilePointer, ''); } diff --git a/src/Spout/Writer/XLSX/Writer.php b/src/Spout/Writer/XLSX/Writer.php index 965955a..b38d69e 100644 --- a/src/Spout/Writer/XLSX/Writer.php +++ b/src/Spout/Writer/XLSX/Writer.php @@ -30,6 +30,9 @@ class Writer extends AbstractMultiSheetsWriter /** @var Internal\Workbook The workbook for the XLSX file */ protected $book; + /** @var array contain column width information */ + protected $columnWidths = []; + /** * Sets a custom temporary folder for creating intermediate files/folders. * This must be set before opening the writer. @@ -64,6 +67,46 @@ class Writer extends AbstractMultiSheetsWriter return $this; } + /** + * Clear all column width specification + * @return Writer + */ + public function clearColumnWidths() + { + $this->columnWidths = []; + + if ($this->book) { + $this->book->_setColumnWidths($this->columnWidths); + } + + return $this; + } + + /** + * Add a width definition for the next sheet that will be generated + * @param number $width column width + * @param int $count column range size where this width should take effect (default 1) + * @return Writer + */ + public function setColumnWidth($width, $count = 1) + { + $countExisted = count($this->columnWidths); + $min = $countExisted ? $this->columnWidths[$countExisted - 1]['max'] + 1 : 1; + $max = $min + $count - 1; + + $this->columnWidths[] = [ + 'width' => $width, + 'min' => $min, + 'max' => $max + ]; + + if ($this->book) { + $this->book->_setColumnWidths($this->columnWidths); + } + + return $this; + } + /** * Configures the write and sets the current sheet pointer to a new sheet. * @@ -74,7 +117,7 @@ class Writer extends AbstractMultiSheetsWriter { if (!$this->book) { $tempFolder = ($this->tempFolder) ? : sys_get_temp_dir(); - $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle); + $this->book = new Workbook($tempFolder, $this->shouldUseInlineStrings, $this->shouldCreateNewSheetsAutomatically, $this->defaultRowStyle, $this->columnWidths); $this->book->addNewSheetAndMakeItCurrent(); } }