295 {
296 $fromFormats = array('\-', '\ ');
297 $toFormats = array('-', ' ');
298
299 $underlineStyles = array (
305 );
306 $verticalAlignmentStyles = array (
311 );
312 $horizontalAlignmentStyles = array (
319 );
320
321 $timezoneObj = new DateTimeZone('Europe/London');
322 $GMT = new DateTimeZone('UTC');
323
324
325
326 if (!file_exists($pFilename)) {
328 }
329
330 if (!$this->
canRead($pFilename)) {
332 }
333
335 $namespaces = $xml->getNamespaces(true);
336
338 if (isset($xml->DocumentProperties[0])) {
339 foreach($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
340 switch ($propertyName) {
341 case 'Title' :
342 $docProps->setTitle(self::_convertStringEncoding($propertyValue,$this->_charSet));
343 break;
344 case 'Subject' :
345 $docProps->setSubject(self::_convertStringEncoding($propertyValue,$this->_charSet));
346 break;
347 case 'Author' :
348 $docProps->setCreator(self::_convertStringEncoding($propertyValue,$this->_charSet));
349 break;
350 case 'Created' :
351 $creationDate = strtotime($propertyValue);
352 $docProps->setCreated($creationDate);
353 break;
354 case 'LastAuthor' :
355 $docProps->setLastModifiedBy(self::_convertStringEncoding($propertyValue,$this->_charSet));
356 break;
357 case 'LastSaved' :
358 $lastSaveDate = strtotime($propertyValue);
359 $docProps->setModified($lastSaveDate);
360 break;
361 case 'Company' :
362 $docProps->setCompany(self::_convertStringEncoding($propertyValue,$this->_charSet));
363 break;
364 case 'Category' :
365 $docProps->setCategory(self::_convertStringEncoding($propertyValue,$this->_charSet));
366 break;
367 case 'Manager' :
368 $docProps->setManager(self::_convertStringEncoding($propertyValue,$this->_charSet));
369 break;
370 case 'Keywords' :
371 $docProps->setKeywords(self::_convertStringEncoding($propertyValue,$this->_charSet));
372 break;
373 case 'Description' :
374 $docProps->setDescription(self::_convertStringEncoding($propertyValue,$this->_charSet));
375 break;
376 }
377 }
378 }
379 if (isset($xml->CustomDocumentProperties)) {
380 foreach($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) {
381 $propertyAttributes = $propertyValue->attributes($namespaces['dt']);
382 $propertyName = preg_replace_callback('/_x([0-9a-z]{4})_/','PHPExcel_Reader_Excel2003XML::_hex2str',$propertyName);
384 switch((string) $propertyAttributes) {
385 case 'string' :
387 $propertyValue = trim($propertyValue);
388 break;
389 case 'boolean' :
391 $propertyValue = (bool) $propertyValue;
392 break;
393 case 'integer' :
395 $propertyValue = intval($propertyValue);
396 break;
397 case 'float' :
399 $propertyValue = floatval($propertyValue);
400 break;
401 case 'dateTime.tz' :
403 $propertyValue = strtotime(trim($propertyValue));
404 break;
405 }
406 $docProps->setCustomProperty($propertyName,$propertyValue,$propertyType);
407 }
408 }
409
410 foreach($xml->Styles[0] as
$style) {
411 $style_ss =
$style->attributes($namespaces[
'ss']);
412 $styleID = (string) $style_ss['ID'];
413
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
422 switch ($styleType) {
423 case 'Alignment' :
424 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
425
426 $styleAttributeValue = (string) $styleAttributeValue;
427 switch ($styleAttributeKey) {
428 case 'Vertical' :
429 if (self::identifyFixedStyleValue($verticalAlignmentStyles,$styleAttributeValue)) {
430 $this->_styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
431 }
432 break;
433 case 'Horizontal' :
434 if (self::identifyFixedStyleValue($horizontalAlignmentStyles,$styleAttributeValue)) {
435 $this->_styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
436 }
437 break;
438 case 'WrapText' :
439 $this->_styles[$styleID]['alignment']['wrap'] = true;
440 break;
441 }
442 }
443 break;
444 case 'Borders' :
445 foreach($styleData->Border as $borderStyle) {
446 $borderAttributes = $borderStyle->attributes($namespaces['ss']);
447 $thisBorder = array();
448 foreach($borderAttributes as $borderStyleKey => $borderStyleValue) {
449
450 switch ($borderStyleKey) {
451 case 'LineStyle' :
453
454 break;
455 case 'Weight' :
456
457 break;
458 case 'Position' :
459 $borderPosition = strtolower($borderStyleValue);
460 break;
461 case 'Color' :
462 $borderColour = substr($borderStyleValue,1);
463 $thisBorder['color']['rgb'] = $borderColour;
464 break;
465 }
466 }
467 if (!empty($thisBorder)) {
468 if (($borderPosition == 'left') || ($borderPosition == 'right') || ($borderPosition == 'top') || ($borderPosition == 'bottom')) {
469 $this->_styles[$styleID]['borders'][$borderPosition] = $thisBorder;
470 }
471 }
472 }
473 break;
474 case 'Font' :
475 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
476
477 $styleAttributeValue = (string) $styleAttributeValue;
478 switch ($styleAttributeKey) {
479 case 'FontName' :
480 $this->_styles[$styleID]['font']['name'] = $styleAttributeValue;
481 break;
482 case 'Size' :
483 $this->_styles[$styleID]['font']['size'] = $styleAttributeValue;
484 break;
485 case 'Color' :
486 $this->_styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue,1);
487 break;
488 case 'Bold' :
489 $this->_styles[$styleID]['font']['bold'] = true;
490 break;
491 case 'Italic' :
492 $this->_styles[$styleID]['font']['italic'] = true;
493 break;
494 case 'Underline' :
495 if (self::identifyFixedStyleValue($underlineStyles,$styleAttributeValue)) {
496 $this->_styles[$styleID]['font']['underline'] = $styleAttributeValue;
497 }
498 break;
499 }
500 }
501 break;
502 case 'Interior' :
503 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
504
505 switch ($styleAttributeKey) {
506 case 'Color' :
507 $this->_styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue,1);
508 break;
509 }
510 }
511 break;
512 case 'NumberFormat' :
513 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
514
515 $styleAttributeValue = str_replace($fromFormats,$toFormats,$styleAttributeValue);
516 switch ($styleAttributeValue) {
517 case 'Short Date' :
518 $styleAttributeValue = 'dd/mm/yyyy';
519 break;
520 }
521 if ($styleAttributeValue > '') {
522 $this->_styles[$styleID]['numberformat']['code'] = $styleAttributeValue;
523 }
524 }
525 break;
526 case 'Protection' :
527 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
528
529 }
530 break;
531 }
532 }
533
534
535 }
536
537
538 $worksheetID = 0;
539 $xml_ss = $xml->children($namespaces['ss']);
540
542 $worksheet_ss =
$worksheet->attributes($namespaces[
'ss']);
543
544 if ((isset($this->_loadSheetsOnly)) && (isset($worksheet_ss['Name'])) &&
545 (!in_array($worksheet_ss['Name'], $this->_loadSheetsOnly))) {
546 continue;
547 }
548
549
550
551
554 if (isset($worksheet_ss['Name'])) {
556
557
558
559 $objPHPExcel->getActiveSheet()->setTitle($worksheetName,
false);
560 }
561
562 $columnID = 'A';
564 foreach(
$worksheet->Table->Column as $columnData) {
565 $columnData_ss = $columnData->attributes($namespaces['ss']);
566 if (isset($columnData_ss['Index'])) {
568 }
569 if (isset($columnData_ss['Width'])) {
570 $columnWidth = $columnData_ss['Width'];
571
572 $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
573 }
574 ++$columnID;
575 }
576 }
577
578 $rowID = 1;
580 $additionalMergedCells = 0;
582 $rowHasData = false;
583 $row_ss = $rowData->attributes($namespaces['ss']);
584 if (isset($row_ss['Index'])) {
585 $rowID = (integer) $row_ss['Index'];
586 }
587
588
589 $columnID = 'A';
590 foreach($rowData->Cell as $cell) {
591
592 $cell_ss = $cell->attributes($namespaces['ss']);
593 if (isset($cell_ss['Index'])) {
595 }
596 $cellRange = $columnID.$rowID;
597
599 if (!$this->
getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
600 continue;
601 }
602 }
603
604 if ((isset($cell_ss['MergeAcross'])) || (isset($cell_ss['MergeDown']))) {
605 $columnTo = $columnID;
606 if (isset($cell_ss['MergeAcross'])) {
607 $additionalMergedCells += (int)$cell_ss['MergeAcross'];
609 }
610 $rowTo = $rowID;
611 if (isset($cell_ss['MergeDown'])) {
612 $rowTo = $rowTo + $cell_ss['MergeDown'];
613 }
614 $cellRange .= ':'.$columnTo.$rowTo;
616 }
617
618 $cellIsSet = $hasCalculatedValue = false;
619 $cellDataFormula = '';
620 if (isset($cell_ss['Formula'])) {
621 $cellDataFormula = $cell_ss['Formula'];
622
623 if (isset($cell_ss['ArrayRange'])) {
624 $cellDataCSEFormula = $cell_ss['ArrayRange'];
625
626 }
627 $hasCalculatedValue = true;
628 }
629 if (isset($cell->Data)) {
630 $cellValue = $cellData = $cell->Data;
632 $cellData_ss = $cellData->attributes($namespaces['ss']);
633 if (isset($cellData_ss['Type'])) {
634 $cellDataType = $cellData_ss['Type'];
635 switch ($cellDataType) {
636
637
638
639
640
641
642
643
644
645 case 'String' :
648 break;
649 case 'Number' :
651 $cellValue = (float) $cellValue;
652 if (floor($cellValue) == $cellValue) {
653 $cellValue = (integer) $cellValue;
654 }
655 break;
656 case 'Boolean' :
658 $cellValue = ($cellValue != 0);
659 break;
660 case 'DateTime' :
663 break;
664 case 'Error' :
666 break;
667 }
668 }
669
670 if ($hasCalculatedValue) {
671
674 if (substr($cellDataFormula,0,3) == 'of:') {
675 $cellDataFormula = substr($cellDataFormula,3);
676
677 $temp = explode('"',$cellDataFormula);
678 $key = false;
679 foreach($temp as &$value) {
680
681 if ($key = !$key) {
682 $value = str_replace(array('[.','.',']'),'',$value);
683 }
684 }
685 } else {
686
687
688 $temp = explode('"',$cellDataFormula);
689 $key = false;
690 foreach($temp as &$value) {
691
692 if ($key = !$key) {
693 preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
694
695
696
697 $cellReferences = array_reverse($cellReferences);
698
699
700 foreach($cellReferences as $cellReference) {
701 $rowReference = $cellReference[2][0];
702
703 if ($rowReference == '') $rowReference = $rowID;
704
705 if ($rowReference{0} == '[') $rowReference = $rowID + trim($rowReference,'[]');
706 $columnReference = $cellReference[4][0];
707
708 if ($columnReference == '') $columnReference = $columnNumber;
709
710 if ($columnReference{0} == '[') $columnReference = $columnNumber + trim($columnReference,'[]');
712 $value = substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
713 }
714 }
715 }
716 }
717 unset($value);
718
719 $cellDataFormula = implode('"',$temp);
720
721 }
722
723
724
725 $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue),$type);
726 if ($hasCalculatedValue) {
727
728 $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setCalculatedValue($cellValue);
729 }
730 $cellIsSet = $rowHasData = true;
731 }
732
733 if (isset($cell->Comment)) {
734
735 $commentAttributes = $cell->Comment->attributes($namespaces['ss']);
736 $author = 'unknown';
737 if (isset($commentAttributes->Author)) {
738 $author = (string)$commentAttributes->Author;
739
740 }
741 $node = $cell->Comment->Data->asXML();
742
743
744 $annotation = strip_tags($node);
745
746 $objPHPExcel->getActiveSheet()->getComment( $columnID.$rowID )
747 ->setAuthor(self::_convertStringEncoding($author ,$this->_charSet))
749 }
750
751 if (($cellIsSet) && (isset($cell_ss['StyleID']))) {
752 $style = (string) $cell_ss[
'StyleID'];
753
754 if ((isset($this->_styles[
$style])) && (!empty($this->_styles[
$style]))) {
755
756
757
758 if (!
$objPHPExcel->getActiveSheet()->cellExists($columnID.$rowID)) {
759 $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValue(NULL);
760 }
761 $objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->_styles[
$style]);
762 }
763 }
764 ++$columnID;
765 while ($additionalMergedCells > 0) {
766 ++$columnID;
767 $additionalMergedCells--;
768 }
769 }
770
771 if ($rowHasData) {
772 if (isset($row_ss['StyleID'])) {
773 $rowStyle = $row_ss['StyleID'];
774 }
775 if (isset($row_ss['Height'])) {
776 $rowHeight = $row_ss['Height'];
777
778 $objPHPExcel->getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight);
779 }
780 }
781
782 ++$rowID;
783 }
784 }
785 ++$worksheetID;
786 }
787
788
790 }
static columnIndexFromString($pString='A')
Column index from string.
const PROPERTY_TYPE_BOOLEAN
constants
const PROPERTY_TYPE_UNKNOWN
const PROPERTY_TYPE_FLOAT
const PROPERTY_TYPE_INTEGER
const PROPERTY_TYPE_STRING
getReadFilter()
Read filter.
static PHPToExcel($dateValue=0, $adjustToTimezone=FALSE, $timezone=NULL)
Convert a date from PHP to Excel.
const HORIZONTAL_CENTER_CONTINUOUS
const UNDERLINE_SINGLEACCOUNTING
const UNDERLINE_DOUBLEACCOUNTING