diff --git a/src/Spout/Writer/Common/Internal/AbstractWorkbook.php b/src/Spout/Writer/Common/Internal/AbstractWorkbook.php index c8f9f9f..ca16a29 100644 --- a/src/Spout/Writer/Common/Internal/AbstractWorkbook.php +++ b/src/Spout/Writer/Common/Internal/AbstractWorkbook.php @@ -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. */ diff --git a/src/Spout/Writer/Style/Style.php b/src/Spout/Writer/Style/Style.php index 91e9475..a4254d7 100644 --- a/src/Spout/Writer/Style/Style.php +++ b/src/Spout/Writer/Style/Style.php @@ -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 diff --git a/src/Spout/Writer/Style/StyleBuilder.php b/src/Spout/Writer/Style/StyleBuilder.php index 4619f43..8521a1c 100644 --- a/src/Spout/Writer/Style/StyleBuilder.php +++ b/src/Spout/Writer/Style/StyleBuilder.php @@ -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. * diff --git a/src/Spout/Writer/XLSX/Helper/StyleHelper.php b/src/Spout/Writer/XLSX/Helper/StyleHelper.php index f3da2b5..789c72a 100644 --- a/src/Spout/Writer/XLSX/Helper/StyleHelper.php +++ b/src/Spout/Writer/XLSX/Helper/StyleHelper.php @@ -25,6 +25,7 @@ class StyleHelper extends AbstractStyleHelper 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[] = ''; + } + } + + if ($numberFormatCount == 0){ + return ''; + } + + $content = ''; + $content .= implode('', $formats); + $content .= ''; + return $content; + } + /** * Returns the content of the "" section. * @@ -139,7 +165,11 @@ EOD; $content = ''; foreach ($registeredStyles as $style) { - $content .= 'getNumberFormatId().'" fontId="' . $style->getId() . '" fillId="0" borderId="0" xfId="0"'; + + if ($style->shouldApplyNumberFormat()) { + $content .= ' applyNumberFormat="1"'; + } if ($style->shouldApplyFont()) { $content .= ' applyFont="1"'; diff --git a/src/Spout/Writer/XLSX/Internal/Workbook.php b/src/Spout/Writer/XLSX/Internal/Workbook.php index 5208d4f..9b9ab24 100644 --- a/src/Spout/Writer/XLSX/Internal/Workbook.php +++ b/src/Spout/Writer/XLSX/Internal/Workbook.php @@ -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 */ diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index af3fbc4..ab959c6 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -133,10 +133,18 @@ EOD; $rowXML = ''; - 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 = 'getId() . '"'; + $cellXML .= ' s="' . $cellStyle->getId() . '"'; if (CellHelper::isNonEmptyString($cellValue)) { if ($this->shouldUseInlineStrings) { diff --git a/src/Spout/Writer/XLSX/Writer.php b/src/Spout/Writer/XLSX/Writer.php index 965955a..d61040e 100644 --- a/src/Spout/Writer/XLSX/Writer.php +++ b/src/Spout/Writer/XLSX/Writer.php @@ -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. *