diff --git a/src/Spout/Writer/XLSX/Internal/Workbook.php b/src/Spout/Writer/XLSX/Internal/Workbook.php index 5208d4f..9d97f91 100644 --- a/src/Spout/Writer/XLSX/Internal/Workbook.php +++ b/src/Spout/Writer/XLSX/Internal/Workbook.php @@ -35,6 +35,9 @@ 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 = array(); + /** * @param string $tempFolder * @param bool $shouldUseInlineStrings @@ -42,7 +45,7 @@ class Workbook extends AbstractWorkbook * @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, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle) + public function __construct($tempFolder, $shouldUseInlineStrings, $shouldCreateNewSheetsAutomatically, $defaultRowStyle, $columnwidths) { parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle); @@ -56,6 +59,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 +91,22 @@ class Workbook extends AbstractWorkbook $sheet = new Sheet($newSheetIndex); $worksheetFilesFolder = $this->fileSystemHelper->getXlWorksheetsFolder(); - $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $this->shouldUseInlineStrings); + $worksheet = new Worksheet($sheet, $worksheetFilesFolder, $this->sharedStringsHelper, $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 _setColumnWidth( $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 67e913c..e82e606 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -49,7 +49,7 @@ EOD; * @param bool $shouldUseInlineStrings Whether inline or shared strings should be used * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing */ - public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $shouldUseInlineStrings) + public function __construct($externalSheet, $worksheetFilesFolder, $sharedStringsHelper, $shouldUseInlineStrings, $columnwidths) { $this->externalSheet = $externalSheet; $this->sharedStringsHelper = $sharedStringsHelper; @@ -59,7 +59,7 @@ EOD; $this->stringsEscaper = \Box\Spout\Common\Escaper\XLSX::getInstance(); $this->worksheetFilePath = $worksheetFilesFolder . '/' . strtolower($this->externalSheet->getName()) . '.xml'; - $this->startSheet(); + $this->startSheet( $columnwidths ); } /** @@ -68,12 +68,24 @@ EOD; * @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 = null ) { $this->sheetFilePointer = fopen($this->worksheetFilePath, 'w'); $this->throwIfSheetFilePointerIsNotAvailable(); fwrite($this->sheetFilePointer, self::SHEET_XML_FILE_HEADER); + + if( !empty($columnwidths) ) { + foreach( $columnwidths as $c ) { + fwrite($this->sheetFilePointer, + '' + ); + } + } + fwrite($this->sheetFilePointer, ''); } diff --git a/src/Spout/Writer/XLSX/Writer.php b/src/Spout/Writer/XLSX/Writer.php index 965955a..16e9f33 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 = array(); + /** * Sets a custom temporary folder for creating intermediate files/folders. * This must be set before opening the writer. @@ -64,6 +67,44 @@ class Writer extends AbstractMultiSheetsWriter return $this; } + /** + * Clear all column width specification + * @return Writer + */ + public function clearColumnWidth() { + + $this->columnwidths = array(); + + if( $this->book ) + $this->book->_setColumnWidth( $this->columnwidths ); + + return $this; + } + + /** + * Add a width definition for the next sheet that will be generated + * @param number $width column width + * @param number $min column position ( A=1 ) where this width should take effect + * @param number $max end of range where width take effect ( default to min ) + * @return Writer + */ + public function setColumnsWidth($width, $min, $max = null) { + + if( $max === null ) + $max = $min; + + $this->columnwidths[] = array( + 'width' => $width, + 'min' => $min, + 'max' => $max + ); + + if( $this->book ) + $this->book->_setColumnWidth( $this->columnwidths ); + + return $this; + } + /** * Configures the write and sets the current sheet pointer to a new sheet. * @@ -74,7 +115,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(); } }