/** * Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section * * @param \Box\Spout\Writer\Style\Style $style * @return string */ protected function getStyleSectionContent($style) { $defaultStyle = $this->getDefaultStyle(); $styleIndex = $style->getId() + 1; // 1-based $content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">'; if ($style->shouldApplyFont()) { $content .= '<style:text-properties'; $fontColor = $style->getFontColor(); if ($fontColor !== $defaultStyle->getFontColor()) { $content .= ' fo:color="#' . $fontColor . '"'; } $fontName = $style->getFontName(); if ($fontName !== $defaultStyle->getFontName()) { $content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"'; } $fontSize = $style->getFontSize(); if ($fontSize !== $defaultStyle->getFontSize()) { $content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"'; } if ($style->isFontBold()) { $content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"'; } if ($style->isFontItalic()) { $content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"'; } if ($style->isFontUnderline()) { $content .= ' style:text-underline-style="solid" style:text-underline-type="single"'; } if ($style->isFontStrikethrough()) { $content .= ' style:text-line-through-style="solid"'; } $content .= '/>'; } if ($style->shouldWrapText()) { $content .= '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>'; } if ($style->shouldApplyBorder()) { $borderProperty = '<style:table-cell-properties %s />'; $borders = array_map(function (BorderPart $borderPart) { return BorderHelper::serializeBorderPart($borderPart); }, $style->getBorder()->getParts()); $content .= sprintf($borderProperty, implode(' ', $borders)); } if ($style->shouldApplyBackgroundColor()) { $content .= sprintf(' <style:table-cell-properties fo:background-color="#%s"/>', $style->getBackgroundColor()); } $content .= '</style:style>'; return $content; }
/** * Set the "wrap text" option if a cell of the given row contains a new line. * * @NOTE: There is a bug on the Mac version of Excel (2011 and below) where new lines * are ignored even when the "wrap text" option is set. This only occurs with * inline strings (shared strings do work fine). * A workaround would be to encode "\n" as "_x000D_" but it does not work * on the Windows version of Excel... * * @param \Box\Spout\Writer\Style\Style $style The original style * @param array $dataRow The row the style will be applied to * @return \Box\Spout\Writer\Style\Style The eventually updated style */ protected function applyWrapTextIfCellContainsNewLine($style, $dataRow) { // if the "wrap text" option is already set, no-op if ($style->shouldWrapText()) { return $style; } foreach ($dataRow as $cell) { if (is_string($cell) && strpos($cell, "\n") !== false) { $style->setShouldWrapText(); break; } } return $style; }
/** * Returns the contents of the "<style:table-cell-properties>" section, inside "<style:style>" section * * @param \Box\Spout\Writer\Style\Style $style * @return string */ private function getTableCellPropertiesSectionContent($style) { $content = ''; if ($style->shouldWrapText()) { $content .= $this->getWrapTextXMLContent(); } if ($style->shouldApplyBorder()) { $content .= $this->getBorderXMLContent($style); } if ($style->shouldApplyBackgroundColor()) { $content .= $this->getBackgroundColorXMLContent($style); } return $content; }
/** * Returns the contents of the "<style:style>" section, inside "<office:automatic-styles>" section * * @param \Box\Spout\Writer\Style\Style $style * @return string */ protected function getStyleSectionContent($style) { $defaultStyle = $this->getDefaultStyle(); $styleIndex = $style->getId() + 1; // 1-based $content = '<style:style style:data-style-name="N0" style:family="table-cell" style:name="ce' . $styleIndex . '" style:parent-style-name="Default">'; if ($style->shouldApplyFont()) { $content .= '<style:text-properties'; $fontColor = $style->getFontColor(); if ($fontColor !== $defaultStyle->getFontColor()) { $content .= ' fo:color="#' . $fontColor . '"'; } $fontName = $style->getFontName(); if ($fontName !== $defaultStyle->getFontName()) { $content .= ' style:font-name="' . $fontName . '" style:font-name-asian="' . $fontName . '" style:font-name-complex="' . $fontName . '"'; } $fontSize = $style->getFontSize(); if ($fontSize !== $defaultStyle->getFontSize()) { $content .= ' fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt"'; } if ($style->isFontBold()) { $content .= ' fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"'; } if ($style->isFontItalic()) { $content .= ' fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"'; } if ($style->isFontUnderline()) { $content .= ' style:text-underline-style="solid" style:text-underline-type="single"'; } if ($style->isFontStrikethrough()) { $content .= ' style:text-line-through-style="solid"'; } $content .= '/>'; } if ($style->shouldWrapText()) { $content .= '<style:table-cell-properties fo:wrap-option="wrap" style:vertical-align="automatic"/>'; } $content .= '</style:style>'; return $content; }