Added support for cell formats when writing Excel files
This commit is contained in:
1 parent
16a2f91a22
commit
40aecd7b90
6 files changed
+277
-2
No files matched your search
@@ -33,7 +33,10 @@ class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
|
||||
$associatedBorderId = $this->styleRegistry->getBorderIdForStyleId($styleId);
|
||||
$hasStyleCustomBorders = ($associatedBorderId !== null && $associatedBorderId !== 0);
|
||||
|
||||
return ($hasStyleCustomFill || $hasStyleCustomBorders);
|
||||
$associatedFormatId = $this->styleRegistry->getFormatIdForStyleId($styleId);
|
||||
$hasStyleCustomFormats = ($associatedFormatId !== null && $associatedFormatId !== 0);
|
||||
|
||||
return ($hasStyleCustomFill || $hasStyleCustomBorders || $hasStyleCustomFormats);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,6 +51,7 @@ class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
|
||||
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
||||
EOD;
|
||||
|
||||
$content .= $this->getFormatsSectionContent();
|
||||
$content .= $this->getFontsSectionContent();
|
||||
$content .= $this->getFillsSectionContent();
|
||||
$content .= $this->getBordersSectionContent();
|
||||
@@ -62,6 +66,35 @@ EOD;
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "<numFmts>" section.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getFormatsSectionContent()
|
||||
{
|
||||
$tags = [];
|
||||
$registeredFormats = $this->styleRegistry->getRegisteredFormats();
|
||||
foreach ($registeredFormats as $styleId) {
|
||||
$numFmtId = $this->styleRegistry->getFormatIdForStyleId($styleId);
|
||||
|
||||
//Built-in formats do not need to be declared, skip them
|
||||
if ($numFmtId < 164) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Style $style */
|
||||
$style = $this->styleRegistry->getStyleFromStyleId($styleId);
|
||||
$format = $style->getFormat();
|
||||
$tags[] = '<numFmt numFmtId="' . $numFmtId . '" formatCode="' . $format . '"/>';
|
||||
}
|
||||
$content = '<numFmts count="' . count($tags) . '">';
|
||||
$content .= implode('', $tags);
|
||||
$content .= '</numFmts>';
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "<fonts>" section.
|
||||
*
|
||||
@@ -206,8 +239,9 @@ EOD;
|
||||
$styleId = $style->getId();
|
||||
$fillId = $this->getFillIdForStyleId($styleId);
|
||||
$borderId = $this->getBorderIdForStyleId($styleId);
|
||||
$numFmtId = $this->getFormatIdForStyleId($styleId);
|
||||
|
||||
$content .= '<xf numFmtId="0" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"';
|
||||
$content .= '<xf numFmtId="' . $numFmtId . '" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"';
|
||||
|
||||
if ($style->shouldApplyFont()) {
|
||||
$content .= ' applyFont="1"';
|
||||
@@ -261,6 +295,22 @@ EOD;
|
||||
return $isDefaultStyle ? 0 : ($this->styleRegistry->getBorderIdForStyleId($styleId) ?: 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the format ID associated to the given style ID.
|
||||
* For the default style use general format.
|
||||
*
|
||||
* @param int $styleId
|
||||
* @return int
|
||||
*/
|
||||
private function getFormatIdForStyleId($styleId)
|
||||
{
|
||||
// For the default style (ID = 0), we don't want to override the format.
|
||||
// Otherwise all cells of the spreadsheet will have a format.
|
||||
$isDefaultStyle = ($styleId === 0);
|
||||
|
||||
return $isDefaultStyle ? 0 : ($this->styleRegistry->getFormatIdForStyleId($styleId) ?: 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the "<cellStyles>" section.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user