add column widths and merged cell functionality to spout
This commit is contained in:
parent
738ea30f35
commit
a6a8b9e80f
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace Box\Spout\Writer\Common\Entity;
|
namespace Box\Spout\Writer\Common\Entity;
|
||||||
|
|
||||||
|
use Box\Spout\Common\Exception\IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Worksheet
|
* Class Worksheet
|
||||||
* Entity describing a Worksheet
|
* Entity describing a Worksheet
|
||||||
@ -23,6 +25,14 @@ class Worksheet
|
|||||||
/** @var int Index of the last written row */
|
/** @var int Index of the last written row */
|
||||||
private $lastWrittenRowIndex;
|
private $lastWrittenRowIndex;
|
||||||
|
|
||||||
|
private $colWidths;
|
||||||
|
|
||||||
|
private $defaultColWidth;
|
||||||
|
|
||||||
|
private $defaultRowHeight;
|
||||||
|
|
||||||
|
private $merges;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Worksheet constructor.
|
* Worksheet constructor.
|
||||||
*
|
*
|
||||||
@ -110,4 +120,120 @@ class Worksheet
|
|||||||
// sheet index is zero-based, while ID is 1-based
|
// sheet index is zero-based, while ID is 1-based
|
||||||
return $this->externalSheet->getIndex() + 1;
|
return $this->externalSheet->getIndex() + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets default column width --- Must be set before WorksheetManager->startSheet() is called on this sheet
|
||||||
|
* @param string $col in letter format eg A or AC
|
||||||
|
* @param float $width
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public function setColWidth(string $col, float $width)
|
||||||
|
{
|
||||||
|
$this->throwIfSheetFilePointerIsAlreadyCreated();
|
||||||
|
$this->colWidths[$col] = $width;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets default column width --- Must be set before WorksheetManager->startSheet() is called on this sheet
|
||||||
|
* @param float $width
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public function setDefaultColWidth(float $width)
|
||||||
|
{
|
||||||
|
$this->throwIfSheetFilePointerIsAlreadyCreated();
|
||||||
|
$this->defaultColWidth = $width;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets default row height --- Must be set before WorksheetManager->startSheet() is called on this sheet
|
||||||
|
* @param float $height
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
public function setDefaultRowHeight(float $height)
|
||||||
|
{
|
||||||
|
$this->throwIfSheetFilePointerIsAlreadyCreated();
|
||||||
|
$this->defaultRowHeight = $height;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* merge cells params should be letter and number cell reference eg A3, A5
|
||||||
|
* @param string $leftCell
|
||||||
|
* @param string $rightCell
|
||||||
|
*/
|
||||||
|
public function mergeCells(string $leftCell, string $rightCell)
|
||||||
|
{
|
||||||
|
$this->merges[] = $leftCell . ':' . $rightCell;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove merged cell reference.
|
||||||
|
* @param string $leftCell
|
||||||
|
* @param string $rightCell
|
||||||
|
*/
|
||||||
|
public function unMergeCells(string $leftCell, string $rightCell)
|
||||||
|
{
|
||||||
|
$this->merges = array_diff($this->merges,[$leftCell . ':' . $rightCell]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* used by WorksheetManager to get default row height and width xml to inject into worksheet xml file
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getDefaultXML() : string
|
||||||
|
{
|
||||||
|
if (empty($this->defaultColWidth) && empty($this->defaultRowHeight)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return '<sheetFormatPr' .
|
||||||
|
(empty($this->defaultColWidth) ? '' : ' defaultColWidth="'.$this->defaultColWidth.'"') .
|
||||||
|
(empty($this->defaultRowHeight) ? '' : ' defaultRowHeight="'.$this->defaultRowHeight.'"') .
|
||||||
|
'/>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* used by WorksheetManager to get column width references xml to inject into worksheet xml file
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getColWidthXML()
|
||||||
|
{
|
||||||
|
if (empty($colWidths)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
$xml = '<cols>';
|
||||||
|
foreach ($this->colWidths as $col => $width) {
|
||||||
|
$xml .= '<col min="'.$col.'" max="'.$col.'" width="'.$width.'" style="1" customWidth="1"/>'; //style and customWidth may be unnecessary ??
|
||||||
|
}
|
||||||
|
$xml .= '</cols>';
|
||||||
|
return $xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* used by WorksheetManager to get merged cell references xml to inject into worksheet xml file
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getMergeXML() : string
|
||||||
|
{
|
||||||
|
if (empty($this->merges)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
$xml = '<mergeCells count="'.count($this->merges).'">';
|
||||||
|
foreach ($this->merges as $merge) {
|
||||||
|
$xml .= '<mergeCell ref="'.$merge.'"/>';
|
||||||
|
}
|
||||||
|
$xml .= '</mergeCells>';
|
||||||
|
return $xml;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the sheet has already been started - throws exception
|
||||||
|
*
|
||||||
|
* @throws IOException If the sheet data file is already opened
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function throwIfSheetFilePointerIsAlreadyCreated()
|
||||||
|
{
|
||||||
|
if (!empty($this->filePointer)) {
|
||||||
|
throw new IOException('Trying to add default or column width settings after sheet is created');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,6 +113,8 @@ EOD;
|
|||||||
$worksheet->setFilePointer($sheetFilePointer);
|
$worksheet->setFilePointer($sheetFilePointer);
|
||||||
|
|
||||||
fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
|
fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
|
||||||
|
fwrite($sheetFilePointer, $worksheet->getDefaultXML());
|
||||||
|
fwrite($sheetFilePointer, $worksheet->getColWidthXML());
|
||||||
fwrite($sheetFilePointer, '<sheetData>');
|
fwrite($sheetFilePointer, '<sheetData>');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,6 +270,7 @@ EOD;
|
|||||||
}
|
}
|
||||||
|
|
||||||
fwrite($worksheetFilePointer, '</sheetData>');
|
fwrite($worksheetFilePointer, '</sheetData>');
|
||||||
|
fwrite($worksheetFilePointer, $worksheet->getMergeXML());
|
||||||
fwrite($worksheetFilePointer, '</worksheet>');
|
fwrite($worksheetFilePointer, '</worksheet>');
|
||||||
fclose($worksheetFilePointer);
|
fclose($worksheetFilePointer);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user