diff --git a/src/Spout/Writer/Common/Helper/CellHelper.php b/src/Spout/Writer/Common/Helper/CellHelper.php index 50ead93..9447252 100644 --- a/src/Spout/Writer/Common/Helper/CellHelper.php +++ b/src/Spout/Writer/Common/Helper/CellHelper.php @@ -61,7 +61,7 @@ class CellHelper */ public static function isNonEmptyString($value) { - return (gettype($value) === 'string' && $value !== ''); + return (gettype($value) === 'string' && $value !== '' && !$this->isNumeric($value)); } /** @@ -73,8 +73,7 @@ class CellHelper */ public static function isNumeric($value) { - $valueType = gettype($value); - return ($valueType === 'integer' || $valueType === 'double'); + return preg_match('#^-?\d+\.?\d*$#', $value) === 1; } /** diff --git a/src/Spout/Writer/XLSX/Helper/StyleHelper.php b/src/Spout/Writer/XLSX/Helper/StyleHelper.php index 4a13c95..04be6cb 100644 --- a/src/Spout/Writer/XLSX/Helper/StyleHelper.php +++ b/src/Spout/Writer/XLSX/Helper/StyleHelper.php @@ -151,6 +151,7 @@ class StyleHelper extends AbstractStyleHelper EOD; + $content .= $this->getNumberFormatSectionContent(); $content .= $this->getFontsSectionContent(); $content .= $this->getFillsSectionContent(); $content .= $this->getBordersSectionContent(); @@ -165,6 +166,21 @@ EOD; return $content; } + /** + * Return the content of the "" section + * + * @return string + */ + protected function getNumberFormatSectionContent() + { + $content = << + + +EOL; + return $content; + } + /** * Returns the content of the "" section. * @@ -298,8 +314,9 @@ EOD; protected function getCellXfsSectionContent() { $registeredStyles = $this->getRegisteredStyles(); + $countStyles = count($registeredStyles) + 1; - $content = ''; + $content = ''; foreach ($registeredStyles as $style) { $styleId = $style->getId(); @@ -323,6 +340,9 @@ EOD; } } + // Add default style for numberFormat + $content .= ''; + $content .= ''; return $content; diff --git a/src/Spout/Writer/XLSX/Internal/Worksheet.php b/src/Spout/Writer/XLSX/Internal/Worksheet.php index 029c115..e54119a 100644 --- a/src/Spout/Writer/XLSX/Internal/Worksheet.php +++ b/src/Spout/Writer/XLSX/Internal/Worksheet.php @@ -238,6 +238,8 @@ EOD; */ protected function getCellXML($rowIndex, $cellNumber, $cellValue, $styleId) { + // If $cellValue has double format then assign style from numFmt section (2) else default (1) + $styleId = preg_match('#^\d+\.\d+$#', $cellValue) === 1 ? 2 : $styleId; $columnIndex = CellHelper::getCellIndexFromColumnIndex($cellNumber); $cellXML = '