Add ability to set column widths.

This commit is contained in:
abo 2017-09-14 16:44:38 +03:00
parent 3bbff7ea7d
commit 4b6bf0c2ef
3 changed files with 82 additions and 6 deletions

View File

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

View File

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