30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
35 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
92 $this->_readDataOnly = $pValue;
114 $this->_loadSheetsOnly = is_array($value) ?
115 $value : array($value);
126 $this->_loadSheetsOnly = null;
146 $this->_readFilter = $pValue;
177 '<?xml version="1.0"',
178 '<?mso-application progid="Excel.Sheet"?>'
182 if (!file_exists($pFilename)) {
183 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
187 $fh = fopen($pFilename,
'r');
188 $data = fread($fh, 2048);
192 foreach($signature as $match) {
194 if (strpos(
$data, $match) ===
false) {
212 if (!file_exists($pFilename)) {
213 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
216 $worksheetNames = array();
218 $xml = simplexml_load_file($pFilename);
219 $namespaces = $xml->getNamespaces(
true);
221 $xml_ss = $xml->children($namespaces[
'ss']);
222 foreach($xml_ss->Worksheet as $worksheet) {
223 $worksheet_ss = $worksheet->attributes($namespaces[
'ss']);
224 $worksheetNames[] = $worksheet_ss[
'Name'];
227 return $worksheetNames;
238 public function load($pFilename)
248 $styleAttributeValue = strtolower($styleAttributeValue);
249 foreach($styleList as $style) {
250 if ($styleAttributeValue == strtolower($style)) {
251 $styleAttributeValue = $style;
264 $UNIT_OFFSET_MAP = array(0, 36, 73, 109, 146, 182, 219);
266 $widthUnits = 256 * ($pxs / 7);
267 $widthUnits += $UNIT_OFFSET_MAP[($pxs % 7)];
277 $pixels = ($widthUnits / 256) * 7;
278 $offsetWidthUnits = $widthUnits % 256;
279 $pixels += round($offsetWidthUnits / (256 / 7));
285 return chr(hexdec($hex[1]));
298 $fromFormats = array(
'\-',
'\ ');
299 $toFormats = array(
'-',
' ');
301 $underlineStyles = array (
308 $verticalAlignmentStyles = array (
314 $horizontalAlignmentStyles = array (
323 $timezoneObj =
new DateTimeZone(
'Europe/London');
324 $GMT =
new DateTimeZone(
'UTC');
328 if (!file_exists($pFilename)) {
329 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
332 $xml = simplexml_load_file($pFilename);
333 $namespaces = $xml->getNamespaces(
true);
336 if (isset($xml->DocumentProperties[0])) {
337 foreach($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
338 switch ($propertyName) {
340 $docProps->setTitle($propertyValue);
343 $docProps->setSubject($propertyValue);
346 $docProps->setCreator($propertyValue);
349 $creationDate = strtotime($propertyValue);
350 $docProps->setCreated($creationDate);
353 $docProps->setLastModifiedBy($propertyValue);
356 $lastSaveDate = strtotime($propertyValue);
357 $docProps->setModified($lastSaveDate);
360 $docProps->setCompany($propertyValue);
363 $docProps->setCategory($propertyValue);
366 $docProps->setManager($propertyValue);
369 $docProps->setKeywords($propertyValue);
372 $docProps->setDescription($propertyValue);
377 if (isset($xml->CustomDocumentProperties)) {
378 foreach($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) {
379 $propertyAttributes = $propertyValue->attributes($namespaces[
'dt']);
380 $propertyName = preg_replace_callback(
'/_x([0-9a-z]{4})_/',
'PHPExcel_Reader_Excel2003XML::_hex2str',$propertyName);
382 switch((
string) $propertyAttributes) {
385 $propertyValue = trim($propertyValue);
389 $propertyValue = (bool) $propertyValue;
393 $propertyValue = intval($propertyValue);
397 $propertyValue = floatval($propertyValue);
401 $propertyValue = strtotime(trim($propertyValue));
404 $docProps->setCustomProperty($propertyName,$propertyValue,$propertyType);
408 foreach($xml->Styles[0] as $style) {
409 $style_ss = $style->attributes($namespaces[
'ss']);
410 $styleID = (string) $style_ss[
'ID'];
412 if ($styleID ==
'Default') {
413 $this->_styles[
'Default'] = array();
415 $this->_styles[$styleID] = $this->_styles[
'Default'];
417 foreach ($style as $styleType => $styleData) {
418 $styleAttributes = $styleData->attributes($namespaces[
'ss']);
420 switch ($styleType) {
422 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
424 $styleAttributeValue = (string) $styleAttributeValue;
425 switch ($styleAttributeKey) {
427 if (self::identifyFixedStyleValue($verticalAlignmentStyles,$styleAttributeValue)) {
428 $this->_styles[$styleID][
'alignment'][
'vertical'] = $styleAttributeValue;
432 if (self::identifyFixedStyleValue($horizontalAlignmentStyles,$styleAttributeValue)) {
433 $this->_styles[$styleID][
'alignment'][
'horizontal'] = $styleAttributeValue;
437 $this->_styles[$styleID][
'alignment'][
'wrap'] =
true;
443 foreach($styleData->Border as $borderStyle) {
444 $borderAttributes = $borderStyle->attributes($namespaces[
'ss']);
445 $thisBorder = array();
446 foreach($borderAttributes as $borderStyleKey => $borderStyleValue) {
448 switch ($borderStyleKey) {
457 $borderPosition = strtolower($borderStyleValue);
460 $borderColour = substr($borderStyleValue,1);
461 $thisBorder[
'color'][
'rgb'] = $borderColour;
465 if (count($thisBorder) > 0) {
466 if (($borderPosition ==
'left') || ($borderPosition ==
'right') || ($borderPosition ==
'top') || ($borderPosition ==
'bottom')) {
467 $this->_styles[$styleID][
'borders'][$borderPosition] = $thisBorder;
473 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
475 $styleAttributeValue = (string) $styleAttributeValue;
476 switch ($styleAttributeKey) {
478 $this->_styles[$styleID][
'font'][
'name'] = $styleAttributeValue;
481 $this->_styles[$styleID][
'font'][
'size'] = $styleAttributeValue;
484 $this->_styles[$styleID][
'font'][
'color'][
'rgb'] = substr($styleAttributeValue,1);
487 $this->_styles[$styleID][
'font'][
'bold'] =
true;
490 $this->_styles[$styleID][
'font'][
'italic'] =
true;
493 if (self::identifyFixedStyleValue($underlineStyles,$styleAttributeValue)) {
494 $this->_styles[$styleID][
'font'][
'underline'] = $styleAttributeValue;
501 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
503 switch ($styleAttributeKey) {
505 $this->_styles[$styleID][
'fill'][
'color'][
'rgb'] = substr($styleAttributeValue,1);
510 case 'NumberFormat' :
511 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
513 $styleAttributeValue = str_replace($fromFormats,$toFormats,$styleAttributeValue);
514 switch ($styleAttributeValue) {
516 $styleAttributeValue =
'dd/mm/yyyy';
519 if ($styleAttributeValue >
'') {
520 $this->_styles[$styleID][
'numberformat'][
'code'] = $styleAttributeValue;
525 foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
537 $xml_ss = $xml->children($namespaces[
'ss']);
538 foreach($xml_ss->Worksheet as $worksheet) {
539 $worksheet_ss = $worksheet->attributes($namespaces[
'ss']);
541 if ((isset($this->_loadSheetsOnly)) && (isset($worksheet_ss[
'Name'])) &&
542 (!in_array($worksheet_ss[
'Name'], $this->_loadSheetsOnly))) {
551 if (isset($worksheet_ss[
'Name'])) {
552 $worksheetName = (string) $worksheet_ss[
'Name'];
557 if (isset($worksheet->Table->Column)) {
558 foreach($worksheet->Table->Column as $columnData) {
559 $columnData_ss = $columnData->attributes($namespaces[
'ss']);
560 if (isset($columnData_ss[
'Index'])) {
563 if (isset($columnData_ss[
'Width'])) {
564 $columnWidth = $columnData_ss[
'Width'];
566 $objPHPExcel->
getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
573 if (isset($worksheet->Table->Row)) {
574 foreach($worksheet->Table->Row as $rowData) {
576 $row_ss = $rowData->attributes($namespaces[
'ss']);
577 if (isset($row_ss[
'Index'])) {
578 $rowID = (integer) $row_ss[
'Index'];
583 foreach($rowData->Cell as $cell) {
585 $cell_ss = $cell->attributes($namespaces[
'ss']);
586 if (isset($cell_ss[
'Index'])) {
589 $cellRange = $columnID.$rowID;
592 if (!$this->
getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
597 if ((isset($cell_ss[
'MergeAcross'])) || (isset($cell_ss[
'MergeDown']))) {
598 $columnTo = $columnID;
599 if (isset($cell_ss[
'MergeAcross'])) {
603 if (isset($cell_ss[
'MergeDown'])) {
604 $rowTo = $rowTo + $cell_ss[
'MergeDown'];
606 $cellRange .=
':'.$columnTo.$rowTo;
610 $cellIsSet = $hasCalculatedValue =
false;
611 $cellDataFormula =
'';
612 if (isset($cell_ss[
'Formula'])) {
613 $cellDataFormula = $cell_ss[
'Formula'];
615 if (isset($cell_ss[
'ArrayRange'])) {
616 $cellDataCSEFormula = $cell_ss[
'ArrayRange'];
619 $hasCalculatedValue =
true;
621 if (isset($cell->Data)) {
622 $cellValue = $cellData = $cell->Data;
624 $cellData_ss = $cellData->attributes($namespaces[
'ss']);
625 if (isset($cellData_ss[
'Type'])) {
626 $cellDataType = $cellData_ss[
'Type'];
627 switch ($cellDataType) {
642 $cellValue = (float) $cellValue;
643 if (floor($cellValue) == $cellValue) {
644 $cellValue = (integer) $cellValue;
649 $cellValue = ($cellValue != 0);
661 if ($hasCalculatedValue) {
665 if (substr($cellDataFormula,0,3) ==
'of:') {
666 $cellDataFormula = substr($cellDataFormula,3);
668 $temp = explode(
'"',$cellDataFormula);
670 foreach($temp as &$value) {
673 $value = str_replace(array(
'[.',
'.',
']'),
'',$value);
679 $temp = explode(
'"',$cellDataFormula);
681 foreach($temp as &$value) {
684 preg_match_all(
'/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
688 $cellReferences = array_reverse($cellReferences);
691 foreach($cellReferences as $cellReference) {
692 $rowReference = $cellReference[2][0];
694 if ($rowReference ==
'') $rowReference = $rowID;
696 if ($rowReference{0} ==
'[') $rowReference = $rowID + trim($rowReference,
'[]');
697 $columnReference = $cellReference[4][0];
699 if ($columnReference ==
'') $columnReference = $columnNumber;
701 if ($columnReference{0} ==
'[') $columnReference = $columnNumber + trim($columnReference,
'[]');
703 $value = substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
710 $cellDataFormula = implode(
'"',$temp);
716 $objPHPExcel->
getActiveSheet()->getCell($columnID.$rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue),
$type);
717 if ($hasCalculatedValue) {
719 $objPHPExcel->
getActiveSheet()->getCell($columnID.$rowID)->setCalculatedValue($cellValue);
721 $cellIsSet = $rowHasData =
true;
724 if (isset($cell->Comment)) {
726 $commentAttributes = $cell->Comment->attributes($namespaces[
'ss']);
728 if (isset($commentAttributes->Author)) {
729 $author = (string)$commentAttributes->Author;
732 $node = $cell->Comment->Data->asXML();
735 $annotation = strip_tags($node);
738 ->setAuthor( $author )
742 if (($cellIsSet) && (isset($cell_ss[
'StyleID']))) {
743 $style = (string) $cell_ss[
'StyleID'];
745 if ((isset($this->_styles[$style])) && (count($this->_styles[$style]) > 0)) {
749 if (!$objPHPExcel->
getActiveSheet()->cellExists($columnID.$rowID)) {
750 $objPHPExcel->
getActiveSheet()->getCell($columnID.$rowID)->setValue(NULL);
752 $objPHPExcel->
getActiveSheet()->getStyle($cellRange)->applyFromArray($this->_styles[$style]);
759 if (isset($row_ss[
'StyleID'])) {
760 $rowStyle = $row_ss[
'StyleID'];
762 if (isset($row_ss[
'Height'])) {
763 $rowHeight = $row_ss[
'Height'];
765 $objPHPExcel->
getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight);
782 $value->createText($is);