This commit is contained in:
Your Name
2026-03-01 14:17:59 +08:00
parent 65aa12f146
commit a07e844c47
6071 changed files with 777651 additions and 0 deletions
@@ -0,0 +1,133 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use SimpleXMLElement;
use stdClass;
class PageSettings
{
/**
* @var stdClass
*/
private $printSettings;
public function __construct(SimpleXMLElement $xmlX, array $namespaces)
{
$printSettings = $this->pageSetup($xmlX, $namespaces, $this->getPrintDefaults());
$this->printSettings = $this->printSetup($xmlX, $printSettings);
}
public function loadPageSettings(Spreadsheet $spreadsheet): void
{
$spreadsheet->getActiveSheet()->getPageSetup()
->setPaperSize($this->printSettings->paperSize)
->setOrientation($this->printSettings->orientation)
->setScale($this->printSettings->scale)
->setVerticalCentered($this->printSettings->verticalCentered)
->setHorizontalCentered($this->printSettings->horizontalCentered)
->setPageOrder($this->printSettings->printOrder);
$spreadsheet->getActiveSheet()->getPageMargins()
->setTop($this->printSettings->topMargin)
->setHeader($this->printSettings->headerMargin)
->setLeft($this->printSettings->leftMargin)
->setRight($this->printSettings->rightMargin)
->setBottom($this->printSettings->bottomMargin)
->setFooter($this->printSettings->footerMargin);
}
private function getPrintDefaults(): stdClass
{
return (object) [
'paperSize' => 9,
'orientation' => PageSetup::ORIENTATION_DEFAULT,
'scale' => 100,
'horizontalCentered' => false,
'verticalCentered' => false,
'printOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER,
'topMargin' => 0.75,
'headerMargin' => 0.3,
'leftMargin' => 0.7,
'rightMargin' => 0.7,
'bottomMargin' => 0.75,
'footerMargin' => 0.3,
];
}
private function pageSetup(SimpleXMLElement $xmlX, array $namespaces, stdClass $printDefaults): stdClass
{
if (isset($xmlX->WorksheetOptions->PageSetup)) {
foreach ($xmlX->WorksheetOptions->PageSetup as $pageSetupData) {
foreach ($pageSetupData as $pageSetupKey => $pageSetupValue) {
/** @scrutinizer ignore-call */
$pageSetupAttributes = $pageSetupValue->attributes($namespaces['x']);
if ($pageSetupAttributes !== null) {
switch ($pageSetupKey) {
case 'Layout':
$this->setLayout($printDefaults, $pageSetupAttributes);
break;
case 'Header':
$printDefaults->headerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
break;
case 'Footer':
$printDefaults->footerMargin = (float) $pageSetupAttributes->Margin ?: 1.0;
break;
case 'PageMargins':
$this->setMargins($printDefaults, $pageSetupAttributes);
break;
}
}
}
}
}
return $printDefaults;
}
private function printSetup(SimpleXMLElement $xmlX, stdClass $printDefaults): stdClass
{
if (isset($xmlX->WorksheetOptions->Print)) {
foreach ($xmlX->WorksheetOptions->Print as $printData) {
foreach ($printData as $printKey => $printValue) {
switch ($printKey) {
case 'LeftToRight':
$printDefaults->printOrder = PageSetup::PAGEORDER_OVER_THEN_DOWN;
break;
case 'PaperSizeIndex':
$printDefaults->paperSize = (int) $printValue ?: 9;
break;
case 'Scale':
$printDefaults->scale = (int) $printValue ?: 100;
break;
}
}
}
}
return $printDefaults;
}
private function setLayout(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
{
$printDefaults->orientation = (string) strtolower($pageSetupAttributes->Orientation ?? '') ?: PageSetup::ORIENTATION_PORTRAIT;
$printDefaults->horizontalCentered = (bool) $pageSetupAttributes->CenterHorizontal ?: false;
$printDefaults->verticalCentered = (bool) $pageSetupAttributes->CenterVertical ?: false;
}
private function setMargins(stdClass $printDefaults, SimpleXMLElement $pageSetupAttributes): void
{
$printDefaults->leftMargin = (float) $pageSetupAttributes->Left ?: 1.0;
$printDefaults->rightMargin = (float) $pageSetupAttributes->Right ?: 1.0;
$printDefaults->topMargin = (float) $pageSetupAttributes->Top ?: 1.0;
$printDefaults->bottomMargin = (float) $pageSetupAttributes->Bottom ?: 1.0;
}
}
@@ -0,0 +1,155 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml;
use PhpOffice\PhpSpreadsheet\Document\Properties as DocumentProperties;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use SimpleXMLElement;
class Properties
{
/**
* @var Spreadsheet
*/
protected $spreadsheet;
public function __construct(Spreadsheet $spreadsheet)
{
$this->spreadsheet = $spreadsheet;
}
public function readProperties(SimpleXMLElement $xml, array $namespaces): void
{
$this->readStandardProperties($xml);
$this->readCustomProperties($xml, $namespaces);
}
protected function readStandardProperties(SimpleXMLElement $xml): void
{
if (isset($xml->DocumentProperties[0])) {
$docProps = $this->spreadsheet->getProperties();
foreach ($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
$propertyValue = (string) $propertyValue;
$this->processStandardProperty($docProps, $propertyName, $propertyValue);
}
}
}
protected function readCustomProperties(SimpleXMLElement $xml, array $namespaces): void
{
if (isset($xml->CustomDocumentProperties) && is_iterable($xml->CustomDocumentProperties[0])) {
$docProps = $this->spreadsheet->getProperties();
foreach ($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) {
$propertyAttributes = self::getAttributes($propertyValue, $namespaces['dt']);
$propertyName = (string) preg_replace_callback('/_x([0-9a-f]{4})_/i', [$this, 'hex2str'], $propertyName);
$this->processCustomProperty($docProps, $propertyName, $propertyValue, $propertyAttributes);
}
}
}
protected function processStandardProperty(
DocumentProperties $docProps,
string $propertyName,
string $stringValue
): void {
switch ($propertyName) {
case 'Title':
$docProps->setTitle($stringValue);
break;
case 'Subject':
$docProps->setSubject($stringValue);
break;
case 'Author':
$docProps->setCreator($stringValue);
break;
case 'Created':
$docProps->setCreated($stringValue);
break;
case 'LastAuthor':
$docProps->setLastModifiedBy($stringValue);
break;
case 'LastSaved':
$docProps->setModified($stringValue);
break;
case 'Company':
$docProps->setCompany($stringValue);
break;
case 'Category':
$docProps->setCategory($stringValue);
break;
case 'Manager':
$docProps->setManager($stringValue);
break;
case 'Keywords':
$docProps->setKeywords($stringValue);
break;
case 'Description':
$docProps->setDescription($stringValue);
break;
}
}
protected function processCustomProperty(
DocumentProperties $docProps,
string $propertyName,
?SimpleXMLElement $propertyValue,
SimpleXMLElement $propertyAttributes
): void {
$propertyType = DocumentProperties::PROPERTY_TYPE_UNKNOWN;
switch ((string) $propertyAttributes) {
case 'string':
$propertyType = DocumentProperties::PROPERTY_TYPE_STRING;
$propertyValue = trim((string) $propertyValue);
break;
case 'boolean':
$propertyType = DocumentProperties::PROPERTY_TYPE_BOOLEAN;
$propertyValue = (bool) $propertyValue;
break;
case 'integer':
$propertyType = DocumentProperties::PROPERTY_TYPE_INTEGER;
$propertyValue = (int) $propertyValue;
break;
case 'float':
$propertyType = DocumentProperties::PROPERTY_TYPE_FLOAT;
$propertyValue = (float) $propertyValue;
break;
case 'dateTime.tz':
$propertyType = DocumentProperties::PROPERTY_TYPE_DATE;
$propertyValue = trim((string) $propertyValue);
break;
}
$docProps->setCustomProperty($propertyName, $propertyValue, $propertyType);
}
protected function hex2str(array $hex): string
{
return mb_chr((int) hexdec($hex[1]), 'UTF-8');
}
private static function getAttributes(?SimpleXMLElement $simple, string $node): SimpleXMLElement
{
return ($simple === null) ? new SimpleXMLElement('<xml></xml>') : ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>'));
}
}
@@ -0,0 +1,86 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml;
use SimpleXMLElement;
class Style
{
/**
* Formats.
*
* @var array
*/
protected $styles = [];
public function parseStyles(SimpleXMLElement $xml, array $namespaces): array
{
if (!isset($xml->Styles) || !is_iterable($xml->Styles[0])) {
return [];
}
$alignmentStyleParser = new Style\Alignment();
$borderStyleParser = new Style\Border();
$fontStyleParser = new Style\Font();
$fillStyleParser = new Style\Fill();
$numberFormatStyleParser = new Style\NumberFormat();
foreach ($xml->Styles[0] as $style) {
$style_ss = self::getAttributes($style, $namespaces['ss']);
$styleID = (string) $style_ss['ID'];
$this->styles[$styleID] = $this->styles['Default'] ?? [];
$alignment = $border = $font = $fill = $numberFormat = [];
foreach ($style as $styleType => $styleDatax) {
$styleData = self::getSxml($styleDatax);
$styleAttributes = $styleData->attributes($namespaces['ss']);
switch ($styleType) {
case 'Alignment':
if ($styleAttributes) {
$alignment = $alignmentStyleParser->parseStyle($styleAttributes);
}
break;
case 'Borders':
$border = $borderStyleParser->parseStyle($styleData, $namespaces);
break;
case 'Font':
if ($styleAttributes) {
$font = $fontStyleParser->parseStyle($styleAttributes);
}
break;
case 'Interior':
if ($styleAttributes) {
$fill = $fillStyleParser->parseStyle($styleAttributes);
}
break;
case 'NumberFormat':
if ($styleAttributes) {
$numberFormat = $numberFormatStyleParser->parseStyle($styleAttributes);
}
break;
}
}
$this->styles[$styleID] = array_merge($alignment, $border, $font, $fill, $numberFormat);
}
return $this->styles;
}
private static function getAttributes(?SimpleXMLElement $simple, string $node): SimpleXMLElement
{
return ($simple === null) ? new SimpleXMLElement('<xml></xml>') : ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>'));
}
private static function getSxml(?SimpleXMLElement $simple): SimpleXMLElement
{
return ($simple !== null) ? $simple : new SimpleXMLElement('<xml></xml>');
}
}
@@ -0,0 +1,58 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml\Style;
use PhpOffice\PhpSpreadsheet\Style\Alignment as AlignmentStyles;
use SimpleXMLElement;
class Alignment extends StyleBase
{
protected const VERTICAL_ALIGNMENT_STYLES = [
AlignmentStyles::VERTICAL_BOTTOM,
AlignmentStyles::VERTICAL_TOP,
AlignmentStyles::VERTICAL_CENTER,
AlignmentStyles::VERTICAL_JUSTIFY,
];
protected const HORIZONTAL_ALIGNMENT_STYLES = [
AlignmentStyles::HORIZONTAL_GENERAL,
AlignmentStyles::HORIZONTAL_LEFT,
AlignmentStyles::HORIZONTAL_RIGHT,
AlignmentStyles::HORIZONTAL_CENTER,
AlignmentStyles::HORIZONTAL_CENTER_CONTINUOUS,
AlignmentStyles::HORIZONTAL_JUSTIFY,
];
public function parseStyle(SimpleXMLElement $styleAttributes): array
{
$style = [];
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
$styleAttributeValue = (string) $styleAttributeValue;
switch ($styleAttributeKey) {
case 'Vertical':
if (self::identifyFixedStyleValue(self::VERTICAL_ALIGNMENT_STYLES, $styleAttributeValue)) {
$style['alignment']['vertical'] = $styleAttributeValue;
}
break;
case 'Horizontal':
if (self::identifyFixedStyleValue(self::HORIZONTAL_ALIGNMENT_STYLES, $styleAttributeValue)) {
$style['alignment']['horizontal'] = $styleAttributeValue;
}
break;
case 'WrapText':
$style['alignment']['wrapText'] = true;
break;
case 'Rotate':
$style['alignment']['textRotation'] = $styleAttributeValue;
break;
}
}
return $style;
}
}
@@ -0,0 +1,98 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml\Style;
use PhpOffice\PhpSpreadsheet\Style\Border as BorderStyle;
use PhpOffice\PhpSpreadsheet\Style\Borders;
use SimpleXMLElement;
class Border extends StyleBase
{
protected const BORDER_POSITIONS = [
'top',
'left',
'bottom',
'right',
];
/**
* @var array
*/
public const BORDER_MAPPINGS = [
'borderStyle' => [
'1continuous' => BorderStyle::BORDER_THIN,
'1dash' => BorderStyle::BORDER_DASHED,
'1dashdot' => BorderStyle::BORDER_DASHDOT,
'1dashdotdot' => BorderStyle::BORDER_DASHDOTDOT,
'1dot' => BorderStyle::BORDER_DOTTED,
'1double' => BorderStyle::BORDER_DOUBLE,
'2continuous' => BorderStyle::BORDER_MEDIUM,
'2dash' => BorderStyle::BORDER_MEDIUMDASHED,
'2dashdot' => BorderStyle::BORDER_MEDIUMDASHDOT,
'2dashdotdot' => BorderStyle::BORDER_MEDIUMDASHDOTDOT,
'2dot' => BorderStyle::BORDER_DOTTED,
'2double' => BorderStyle::BORDER_DOUBLE,
'3continuous' => BorderStyle::BORDER_THICK,
'3dash' => BorderStyle::BORDER_MEDIUMDASHED,
'3dashdot' => BorderStyle::BORDER_MEDIUMDASHDOT,
'3dashdotdot' => BorderStyle::BORDER_MEDIUMDASHDOTDOT,
'3dot' => BorderStyle::BORDER_DOTTED,
'3double' => BorderStyle::BORDER_DOUBLE,
],
];
public function parseStyle(SimpleXMLElement $styleData, array $namespaces): array
{
$style = [];
$diagonalDirection = '';
$borderPosition = '';
foreach ($styleData->Border as $borderStyle) {
$borderAttributes = self::getAttributes($borderStyle, $namespaces['ss']);
$thisBorder = [];
$styleType = (string) $borderAttributes->Weight;
$styleType .= strtolower((string) $borderAttributes->LineStyle);
$thisBorder['borderStyle'] = self::BORDER_MAPPINGS['borderStyle'][$styleType] ?? BorderStyle::BORDER_NONE;
foreach ($borderAttributes as $borderStyleKey => $borderStyleValuex) {
$borderStyleValue = (string) $borderStyleValuex;
switch ($borderStyleKey) {
case 'Position':
[$borderPosition, $diagonalDirection] =
$this->parsePosition($borderStyleValue, $diagonalDirection);
break;
case 'Color':
$borderColour = substr($borderStyleValue, 1);
$thisBorder['color']['rgb'] = $borderColour;
break;
}
}
if ($borderPosition) {
$style['borders'][$borderPosition] = $thisBorder;
} elseif ($diagonalDirection) {
$style['borders']['diagonalDirection'] = $diagonalDirection;
$style['borders']['diagonal'] = $thisBorder;
}
}
return $style;
}
protected function parsePosition(string $borderStyleValue, string $diagonalDirection): array
{
$borderStyleValue = strtolower($borderStyleValue);
if (in_array($borderStyleValue, self::BORDER_POSITIONS)) {
$borderPosition = $borderStyleValue;
} elseif ($borderStyleValue === 'diagonalleft') {
$diagonalDirection = $diagonalDirection ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_DOWN;
} elseif ($borderStyleValue === 'diagonalright') {
$diagonalDirection = $diagonalDirection ? Borders::DIAGONAL_BOTH : Borders::DIAGONAL_UP;
}
return [$borderPosition ?? null, $diagonalDirection];
}
}
@@ -0,0 +1,63 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml\Style;
use PhpOffice\PhpSpreadsheet\Style\Fill as FillStyles;
use SimpleXMLElement;
class Fill extends StyleBase
{
/**
* @var array
*/
public const FILL_MAPPINGS = [
'fillType' => [
'solid' => FillStyles::FILL_SOLID,
'gray75' => FillStyles::FILL_PATTERN_DARKGRAY,
'gray50' => FillStyles::FILL_PATTERN_MEDIUMGRAY,
'gray25' => FillStyles::FILL_PATTERN_LIGHTGRAY,
'gray125' => FillStyles::FILL_PATTERN_GRAY125,
'gray0625' => FillStyles::FILL_PATTERN_GRAY0625,
'horzstripe' => FillStyles::FILL_PATTERN_DARKHORIZONTAL, // horizontal stripe
'vertstripe' => FillStyles::FILL_PATTERN_DARKVERTICAL, // vertical stripe
'reversediagstripe' => FillStyles::FILL_PATTERN_DARKUP, // reverse diagonal stripe
'diagstripe' => FillStyles::FILL_PATTERN_DARKDOWN, // diagonal stripe
'diagcross' => FillStyles::FILL_PATTERN_DARKGRID, // diagoanl crosshatch
'thickdiagcross' => FillStyles::FILL_PATTERN_DARKTRELLIS, // thick diagonal crosshatch
'thinhorzstripe' => FillStyles::FILL_PATTERN_LIGHTHORIZONTAL,
'thinvertstripe' => FillStyles::FILL_PATTERN_LIGHTVERTICAL,
'thinreversediagstripe' => FillStyles::FILL_PATTERN_LIGHTUP,
'thindiagstripe' => FillStyles::FILL_PATTERN_LIGHTDOWN,
'thinhorzcross' => FillStyles::FILL_PATTERN_LIGHTGRID, // thin horizontal crosshatch
'thindiagcross' => FillStyles::FILL_PATTERN_LIGHTTRELLIS, // thin diagonal crosshatch
],
];
public function parseStyle(SimpleXMLElement $styleAttributes): array
{
$style = [];
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValuex) {
$styleAttributeValue = (string) $styleAttributeValuex;
switch ($styleAttributeKey) {
case 'Color':
$style['fill']['endColor']['rgb'] = substr($styleAttributeValue, 1);
$style['fill']['startColor']['rgb'] = substr($styleAttributeValue, 1);
break;
case 'PatternColor':
$style['fill']['startColor']['rgb'] = substr($styleAttributeValue, 1);
break;
case 'Pattern':
$lcStyleAttributeValue = strtolower((string) $styleAttributeValue);
$style['fill']['fillType']
= self::FILL_MAPPINGS['fillType'][$lcStyleAttributeValue] ?? FillStyles::FILL_NONE;
break;
}
}
return $style;
}
}
@@ -0,0 +1,79 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml\Style;
use PhpOffice\PhpSpreadsheet\Style\Font as FontUnderline;
use SimpleXMLElement;
class Font extends StyleBase
{
protected const UNDERLINE_STYLES = [
FontUnderline::UNDERLINE_NONE,
FontUnderline::UNDERLINE_DOUBLE,
FontUnderline::UNDERLINE_DOUBLEACCOUNTING,
FontUnderline::UNDERLINE_SINGLE,
FontUnderline::UNDERLINE_SINGLEACCOUNTING,
];
protected function parseUnderline(array $style, string $styleAttributeValue): array
{
if (self::identifyFixedStyleValue(self::UNDERLINE_STYLES, $styleAttributeValue)) {
$style['font']['underline'] = $styleAttributeValue;
}
return $style;
}
protected function parseVerticalAlign(array $style, string $styleAttributeValue): array
{
if ($styleAttributeValue == 'Superscript') {
$style['font']['superscript'] = true;
}
if ($styleAttributeValue == 'Subscript') {
$style['font']['subscript'] = true;
}
return $style;
}
public function parseStyle(SimpleXMLElement $styleAttributes): array
{
$style = [];
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
$styleAttributeValue = (string) $styleAttributeValue;
switch ($styleAttributeKey) {
case 'FontName':
$style['font']['name'] = $styleAttributeValue;
break;
case 'Size':
$style['font']['size'] = $styleAttributeValue;
break;
case 'Color':
$style['font']['color']['rgb'] = substr($styleAttributeValue, 1);
break;
case 'Bold':
$style['font']['bold'] = true;
break;
case 'Italic':
$style['font']['italic'] = true;
break;
case 'Underline':
$style = $this->parseUnderline($style, $styleAttributeValue);
break;
case 'VerticalAlign':
$style = $this->parseVerticalAlign($style, $styleAttributeValue);
break;
}
}
return $style;
}
}
@@ -0,0 +1,33 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml\Style;
use SimpleXMLElement;
class NumberFormat extends StyleBase
{
public function parseStyle(SimpleXMLElement $styleAttributes): array
{
$style = [];
$fromFormats = ['\-', '\ '];
$toFormats = ['-', ' '];
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
$styleAttributeValue = str_replace($fromFormats, $toFormats, $styleAttributeValue);
switch ($styleAttributeValue) {
case 'Short Date':
$styleAttributeValue = 'dd/mm/yyyy';
break;
}
if ($styleAttributeValue > '') {
$style['numberFormat']['formatCode'] = $styleAttributeValue;
}
}
return $style;
}
}
@@ -0,0 +1,30 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xml\Style;
use SimpleXMLElement;
abstract class StyleBase
{
protected static function identifyFixedStyleValue(array $styleList, string &$styleAttributeValue): bool
{
$returnValue = false;
$styleAttributeValue = strtolower($styleAttributeValue);
foreach ($styleList as $style) {
if ($styleAttributeValue == strtolower($style)) {
$styleAttributeValue = $style;
$returnValue = true;
break;
}
}
return $returnValue;
}
protected static function getAttributes(?SimpleXMLElement $simple, string $node): SimpleXMLElement
{
return ($simple === null) ? new SimpleXMLElement('<xml></xml>') : ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>'));
}
}