Skip to content

Commit 428b263

Browse files
committed
PHPOfficeGH-558 Check if Styles exist in Excel2003XML file before iterating
1 parent 4f8c9bf commit 428b263

File tree

1 file changed

+78
-76
lines changed

1 file changed

+78
-76
lines changed

Classes/PHPExcel/Reader/Excel2003XML.php

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function canRead($pFilename)
9595
// Open file
9696
$this->_openFile($pFilename);
9797
$fileHandle = $this->_fileHandle;
98-
98+
9999
// Read sample data (first 2 KB will do)
100100
$data = fread($fileHandle, 2048);
101101
fclose($fileHandle);
@@ -407,61 +407,62 @@ public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
407407
}
408408
}
409409

410-
foreach($xml->Styles[0] as $style) {
411-
$style_ss = $style->attributes($namespaces['ss']);
412-
$styleID = (string) $style_ss['ID'];
413-
// echo 'Style ID = '.$styleID.'<br />';
414-
if ($styleID == 'Default') {
415-
$this->_styles['Default'] = array();
416-
} else {
417-
$this->_styles[$styleID] = $this->_styles['Default'];
418-
}
419-
foreach ($style as $styleType => $styleData) {
420-
$styleAttributes = $styleData->attributes($namespaces['ss']);
421-
// echo $styleType.'<br />';
422-
switch ($styleType) {
423-
case 'Alignment' :
424-
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
425-
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
426-
$styleAttributeValue = (string) $styleAttributeValue;
410+
if (isset($xml->Styles)) {
411+
foreach ($xml->Styles[0] as $style) {
412+
$style_ss = $style->attributes($namespaces['ss']);
413+
$styleID = (string)$style_ss['ID'];
414+
// echo 'Style ID = '.$styleID.'<br />';
415+
if ($styleID == 'Default') {
416+
$this->_styles['Default'] = array();
417+
} else {
418+
$this->_styles[$styleID] = $this->_styles['Default'];
419+
}
420+
foreach ($style as $styleType => $styleData) {
421+
$styleAttributes = $styleData->attributes($namespaces['ss']);
422+
// echo $styleType.'<br />';
423+
switch ($styleType) {
424+
case 'Alignment' :
425+
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
426+
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
427+
$styleAttributeValue = (string)$styleAttributeValue;
427428
switch ($styleAttributeKey) {
428429
case 'Vertical' :
429-
if (self::identifyFixedStyleValue($verticalAlignmentStyles,$styleAttributeValue)) {
430-
$this->_styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
431-
}
432-
break;
430+
if (self::identifyFixedStyleValue($verticalAlignmentStyles, $styleAttributeValue)) {
431+
$this->_styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
432+
}
433+
break;
433434
case 'Horizontal' :
434-
if (self::identifyFixedStyleValue($horizontalAlignmentStyles,$styleAttributeValue)) {
435-
$this->_styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
436-
}
437-
break;
435+
if (self::identifyFixedStyleValue($horizontalAlignmentStyles, $styleAttributeValue)) {
436+
$this->_styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
437+
}
438+
break;
438439
case 'WrapText' :
439-
$this->_styles[$styleID]['alignment']['wrap'] = true;
440-
break;
440+
$this->_styles[$styleID]['alignment']['wrap'] = true;
441+
break;
441442
}
442443
}
443444
break;
444-
case 'Borders' :
445-
foreach($styleData->Border as $borderStyle) {
445+
case 'Borders' :
446+
foreach ($styleData->Border as $borderStyle) {
446447
$borderAttributes = $borderStyle->attributes($namespaces['ss']);
447448
$thisBorder = array();
448-
foreach($borderAttributes as $borderStyleKey => $borderStyleValue) {
449-
// echo $borderStyleKey.' = '.$borderStyleValue.'<br />';
449+
foreach ($borderAttributes as $borderStyleKey => $borderStyleValue) {
450+
// echo $borderStyleKey.' = '.$borderStyleValue.'<br />';
450451
switch ($borderStyleKey) {
451452
case 'LineStyle' :
452-
$thisBorder['style'] = PHPExcel_Style_Border::BORDER_MEDIUM;
453-
// $thisBorder['style'] = $borderStyleValue;
454-
break;
453+
$thisBorder['style'] = PHPExcel_Style_Border::BORDER_MEDIUM;
454+
// $thisBorder['style'] = $borderStyleValue;
455+
break;
455456
case 'Weight' :
456-
// $thisBorder['style'] = $borderStyleValue;
457-
break;
457+
// $thisBorder['style'] = $borderStyleValue;
458+
break;
458459
case 'Position' :
459-
$borderPosition = strtolower($borderStyleValue);
460-
break;
460+
$borderPosition = strtolower($borderStyleValue);
461+
break;
461462
case 'Color' :
462-
$borderColour = substr($borderStyleValue,1);
463-
$thisBorder['color']['rgb'] = $borderColour;
464-
break;
463+
$borderColour = substr($borderStyleValue, 1);
464+
$thisBorder['color']['rgb'] = $borderColour;
465+
break;
465466
}
466467
}
467468
if (!empty($thisBorder)) {
@@ -471,67 +472,68 @@ public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
471472
}
472473
}
473474
break;
474-
case 'Font' :
475-
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
476-
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
477-
$styleAttributeValue = (string) $styleAttributeValue;
475+
case 'Font' :
476+
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
477+
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
478+
$styleAttributeValue = (string)$styleAttributeValue;
478479
switch ($styleAttributeKey) {
479480
case 'FontName' :
480-
$this->_styles[$styleID]['font']['name'] = $styleAttributeValue;
481-
break;
481+
$this->_styles[$styleID]['font']['name'] = $styleAttributeValue;
482+
break;
482483
case 'Size' :
483-
$this->_styles[$styleID]['font']['size'] = $styleAttributeValue;
484-
break;
484+
$this->_styles[$styleID]['font']['size'] = $styleAttributeValue;
485+
break;
485486
case 'Color' :
486-
$this->_styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue,1);
487-
break;
487+
$this->_styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue, 1);
488+
break;
488489
case 'Bold' :
489-
$this->_styles[$styleID]['font']['bold'] = true;
490-
break;
490+
$this->_styles[$styleID]['font']['bold'] = true;
491+
break;
491492
case 'Italic' :
492-
$this->_styles[$styleID]['font']['italic'] = true;
493-
break;
493+
$this->_styles[$styleID]['font']['italic'] = true;
494+
break;
494495
case 'Underline' :
495-
if (self::identifyFixedStyleValue($underlineStyles,$styleAttributeValue)) {
496-
$this->_styles[$styleID]['font']['underline'] = $styleAttributeValue;
497-
}
498-
break;
496+
if (self::identifyFixedStyleValue($underlineStyles, $styleAttributeValue)) {
497+
$this->_styles[$styleID]['font']['underline'] = $styleAttributeValue;
498+
}
499+
break;
499500
}
500501
}
501502
break;
502-
case 'Interior' :
503-
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
504-
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
503+
case 'Interior' :
504+
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
505+
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
505506
switch ($styleAttributeKey) {
506507
case 'Color' :
507-
$this->_styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue,1);
508-
break;
508+
$this->_styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue, 1);
509+
break;
509510
}
510511
}
511512
break;
512-
case 'NumberFormat' :
513-
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
514-
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
515-
$styleAttributeValue = str_replace($fromFormats,$toFormats,$styleAttributeValue);
513+
case 'NumberFormat' :
514+
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
515+
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
516+
$styleAttributeValue = str_replace($fromFormats, $toFormats, $styleAttributeValue);
516517
switch ($styleAttributeValue) {
517518
case 'Short Date' :
518-
$styleAttributeValue = 'dd/mm/yyyy';
519-
break;
519+
$styleAttributeValue = 'dd/mm/yyyy';
520+
break;
520521
}
521522
if ($styleAttributeValue > '') {
522523
$this->_styles[$styleID]['numberformat']['code'] = $styleAttributeValue;
523524
}
524525
}
525526
break;
526-
case 'Protection' :
527-
foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
528-
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
527+
case 'Protection' :
528+
foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
529+
// echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
529530
}
530531
break;
532+
}
531533
}
534+
// print_r($this->_styles[$styleID]);
535+
// echo '<hr />';
532536
}
533-
// print_r($this->_styles[$styleID]);
534-
// echo '<hr />';
535537
}
536538
// echo '<hr />';
537539

0 commit comments

Comments
 (0)