Merge 7e4d56be46fc5bcf9ff6548b45d686fe23bb76a4 into d4e57b1f0d7b5432db05a46e3b14daac78a243b3

This commit is contained in:
silversailor 2016-09-07 12:04:05 +00:00 committed by GitHub
commit 8599192ceb
3 changed files with 74 additions and 6 deletions

View File

@ -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.

View File

@ -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,
'<cols><col min="' . $c['min'] .
'" max="' . $c['max'] .
'" width="' . $c['width'] .
'" customWidth="1"/></cols>'
);
}
}
fwrite($this->sheetFilePointer, '<sheetData>');
}

View File

@ -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();
}
}