Initial commit to support Cell styling and number formats in styles

This commit is contained in:
Gerard Krom 2016-05-04 11:40:11 +02:00
parent e9cd7a397e
commit 4f5218dbc9
7 changed files with 96 additions and 3 deletions

View File

@ -167,6 +167,11 @@ abstract class AbstractWorkbook implements WorkbookInterface
}
}
public function applyStyleToCurrentWorksheet($style) {
$updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, array());
$registeredStyle = $styleHelper->registerStyle($updatedStyle);
}
/**
* @return bool Whether the current worksheet has reached the maximum number of rows per sheet.
*/

View File

@ -61,6 +61,13 @@ class Style
/** @var bool Whether the wrap text property was set */
protected $hasSetWrapText = false;
/** @var string Custom number format */
protected $numberFormat = '';
/** @var boolean Whether specific number format has been set */
protected $hasSetNumberFormat = false;
/** @var integer Holds the number format id */
protected $numberFormatId = 0;
/**
* @return int|null
*/
@ -243,6 +250,35 @@ class Style
return $this->shouldApplyFont;
}
public function setNumberFormat($format)
{
$this->numberFormat = $format;
$this->hasSetNumberFormat = true;
return $this;
}
public function setNumberFormatId($id)
{
$this->numberFormatId = $id;
}
public function shouldApplyNumberFormat()
{
return $this->hasSetNumberFormat;
}
public function getNumberFormatId()
{
return $this->numberFormatId;
}
public function getNumberFormat()
{
return $this->numberFormat;
}
/**
* Serializes the style for future comparison with other styles.
* The ID is excluded from the comparison, as we only care about

View File

@ -121,6 +121,12 @@ class StyleBuilder
return $this;
}
public function setNumberFormat($format)
{
$this->style->setNumberFormat($format);
return $this;
}
/**
* Returns the configured style. The style is cached and can be reused.
*

View File

@ -25,6 +25,7 @@ class StyleHelper extends AbstractStyleHelper
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
EOD;
$content .= $this->getNumberFormatSectionContent();
$content .= $this->getFontsSectionContent();
$content .= $this->getFillsSectionContent();
$content .= $this->getBordersSectionContent();
@ -39,6 +40,31 @@ EOD;
return $content;
}
protected function getNumberFormatSectionContent() {
$formats = array();
$numberFormatCount = 0;
// This is the limit excel holds for the default number formats
$baseNumberFormatId = 163;
foreach($this->getRegisteredStyles() as $style) {
/* If this evals to false we should skip it since it isnt used */
if ($style->shouldApplyNumberFormat()) {
$numberFormatCount++;
$style->setNumberFormatId($baseNumberFormatId + $numberFormatCount);
$formats[] = '<numFmt numFmtId="'.$style->getNumberFormatId().'" formatCode="'.$style->getNumberFormat().'"/>';
}
}
if ($numberFormatCount == 0){
return '';
}
$content = '<numFmts count="'.$numberFormatCount.'">';
$content .= implode('', $formats);
$content .= '</numFmts>';
return $content;
}
/**
* Returns the content of the "<fonts>" section.
*
@ -139,7 +165,11 @@ EOD;
$content = '<cellXfs count="' . count($registeredStyles) . '">';
foreach ($registeredStyles as $style) {
$content .= '<xf numFmtId="0" fontId="' . $style->getId() . '" fillId="0" borderId="0" xfId="0"';
$content .= '<xf numFmtId="'.$style->getNumberFormatId().'" fontId="' . $style->getId() . '" fillId="0" borderId="0" xfId="0"';
if ($style->shouldApplyNumberFormat()) {
$content .= ' applyNumberFormat="1"';
}
if ($style->shouldApplyFont()) {
$content .= ' applyFont="1"';

View File

@ -58,6 +58,10 @@ class Workbook extends AbstractWorkbook
$this->sharedStringsHelper = new SharedStringsHelper($xlFolder);
}
public function registerStyle($style) {
$this->styleHelper->registerStyle($style);
}
/**
* @return \Box\Spout\Writer\XLSX\Helper\StyleHelper Helper to apply styles to XLSX files
*/

View File

@ -133,10 +133,18 @@ EOD;
$rowXML = '<row r="' . $rowIndex . '" spans="1:' . $numCells . '">';
foreach($dataRow as $cellValue) {
foreach($dataRow as $cell) {
if (is_array($cell)) {
$cellValue = $cell[0];
$cellStyle = $cell[1];
} else {
$cellValue = $cell;
$cellStyle = $style;
}
$columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber);
$cellXML = '<c r="' . $columnIndex . $rowIndex . '"';
$cellXML .= ' s="' . $style->getId() . '"';
$cellXML .= ' s="' . $cellStyle->getId() . '"';
if (CellHelper::isNonEmptyString($cellValue)) {
if ($this->shouldUseInlineStrings) {

View File

@ -64,6 +64,10 @@ class Writer extends AbstractMultiSheetsWriter
return $this;
}
public function registerStyle($style) {
$this->book->registerStyle($style);
}
/**
* Configures the write and sets the current sheet pointer to a new sheet.
*