30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
35 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
109 $this->_readDataOnly = $pValue;
136 $this->_loadSheetsOnly = is_array($value) ?
137 $value : array($value);
149 $this->_loadSheetsOnly = null;
169 $this->_readFilter = $pValue;
190 if (!function_exists(
'gzread')) {
195 if (!file_exists($pFilename)) {
196 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
200 $fh = fopen($pFilename,
'r');
201 $data = fread($fh, 2);
204 if (
$data != chr(0x1F).chr(0x8B)) {
218 public function load($pFilename)
229 if (
$file !==
false) {
231 while (!gzeof(
$file)) {
248 if (!file_exists($pFilename)) {
249 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
254 $xml = simplexml_load_string($gFileData);
255 $namespacesMeta = $xml->getNamespaces(
true);
257 $gnmXML = $xml->children($namespacesMeta[
'gnm']);
259 $worksheetNames = array();
261 foreach($gnmXML->Sheets->Sheet as $sheet) {
262 $worksheetNames[] = (string) $sheet->Name;
265 return $worksheetNames;
280 if (!file_exists($pFilename)) {
281 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
284 $timezoneObj =
new DateTimeZone(
'Europe/London');
285 $GMT =
new DateTimeZone(
'UTC');
293 $xml = simplexml_load_string($gFileData);
294 $namespacesMeta = $xml->getNamespaces(
true);
298 $gnmXML = $xml->children($namespacesMeta[
'gnm']);
302 if (isset($namespacesMeta[
'office'])) {
303 $officeXML = $xml->children($namespacesMeta[
'office']);
304 $officeDocXML = $officeXML->{
'document-meta'};
305 $officeDocMetaXML = $officeDocXML->meta;
307 foreach($officeDocMetaXML as $officePropertyData) {
309 $officePropertyDC = array();
310 if (isset($namespacesMeta[
'dc'])) {
311 $officePropertyDC = $officePropertyData->children($namespacesMeta[
'dc']);
313 foreach($officePropertyDC as $propertyName => $propertyValue) {
314 $propertyValue = (string) $propertyValue;
315 switch ($propertyName) {
317 $docProps->setTitle(trim($propertyValue));
320 $docProps->setSubject(trim($propertyValue));
323 $docProps->setCreator(trim($propertyValue));
324 $docProps->setLastModifiedBy(trim($propertyValue));
327 $creationDate = strtotime(trim($propertyValue));
328 $docProps->setCreated($creationDate);
329 $docProps->setModified($creationDate);
332 $docProps->setDescription(trim($propertyValue));
336 $officePropertyMeta = array();
337 if (isset($namespacesMeta[
'meta'])) {
338 $officePropertyMeta = $officePropertyData->children($namespacesMeta[
'meta']);
340 foreach($officePropertyMeta as $propertyName => $propertyValue) {
341 $attributes = $propertyValue->attributes($namespacesMeta[
'meta']);
342 $propertyValue = (string) $propertyValue;
343 switch ($propertyName) {
345 $docProps->setKeywords(trim($propertyValue));
347 case 'initial-creator' :
348 $docProps->setCreator(trim($propertyValue));
349 $docProps->setLastModifiedBy(trim($propertyValue));
351 case 'creation-date' :
352 $creationDate = strtotime(trim($propertyValue));
353 $docProps->setCreated($creationDate);
354 $docProps->setModified($creationDate);
356 case 'user-defined' :
357 list(,$attrName) = explode(
':',$attributes[
'name']);
360 $docProps->setCompany(trim($propertyValue));
363 $docProps->setCategory(trim($propertyValue));
366 $docProps->setManager(trim($propertyValue));
373 } elseif (isset($gnmXML->Summary)) {
374 foreach($gnmXML->Summary->Item as $summaryItem) {
375 $propertyName = $summaryItem->name;
376 $propertyValue = $summaryItem->{
'val-string'};
377 switch ($propertyName) {
379 $docProps->setTitle(trim($propertyValue));
382 $docProps->setDescription(trim($propertyValue));
385 $docProps->setKeywords(trim($propertyValue));
388 $docProps->setCategory(trim($propertyValue));
391 $docProps->setManager(trim($propertyValue));
394 $docProps->setCreator(trim($propertyValue));
395 $docProps->setLastModifiedBy(trim($propertyValue));
398 $docProps->setCompany(trim($propertyValue));
405 foreach($gnmXML->Sheets->Sheet as $sheet) {
406 $worksheetName = (string) $sheet->Name;
408 if ((isset($this->_loadSheetsOnly)) && (!in_array($worksheetName, $this->_loadSheetsOnly))) {
412 $maxRow = $maxCol = 0;
415 $objPHPExcel->createSheet();
416 $objPHPExcel->setActiveSheetIndex($worksheetID);
417 $objPHPExcel->getActiveSheet()->setTitle($worksheetName);
419 if ((!$this->_readDataOnly) && (isset($sheet->PrintInformation))) {
420 if (isset($sheet->PrintInformation->Margins)) {
421 foreach($sheet->PrintInformation->Margins->children(
'gnm',TRUE) as $key => $margin) {
422 $marginAttributes = $margin->attributes();
423 $marginSize = 72 / 100;
424 switch($marginAttributes[
'PrefUnit']) {
426 $marginSize = intval($marginAttributes[
'Points']) / 100;
431 $objPHPExcel->getActiveSheet()->getPageMargins()->setTop($marginSize);
434 $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom($marginSize);
437 $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft($marginSize);
440 $objPHPExcel->getActiveSheet()->getPageMargins()->setRight($marginSize);
443 $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader($marginSize);
446 $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter($marginSize);
453 foreach($sheet->Cells->Cell as $cell) {
454 $cellAttributes = $cell->attributes();
455 $row = (int) $cellAttributes->Row + 1;
456 $column = (
int) $cellAttributes->Col;
459 if ($column > $maxCol) $maxCol = $column;
464 if (!is_null($this->getReadFilter())) {
465 if (!$this->getReadFilter()->readCell($column,
$row, $worksheetName)) {
470 $ValueType = $cellAttributes->ValueType;
471 $ExprID = (string) $cellAttributes->ExprID;
477 if (((
string) $cell) >
'') {
479 $this->_expressions[$ExprID] = array(
'column' => $cellAttributes->Col,
480 'row' => $cellAttributes->Row,
481 'formula' => (
string) $cell
485 $expression = $this->_expressions[$ExprID];
487 $cell = $this->_referenceHelper->updateFormulaReferences( $expression[
'formula'],
489 $cellAttributes->Col - $expression[
'column'],
490 $cellAttributes->Row - $expression[
'row'],
504 $cell = ($cell ==
'TRUE') ? True : False;
507 $cell = intval($cell);
521 $objPHPExcel->getActiveSheet()->getCell($column.$row)->setValueExplicit($cell,
$type);
524 if ((!$this->_readDataOnly) && (isset($sheet->Objects))) {
525 foreach($sheet->Objects->children(
'gnm',TRUE) as $key =>
$comment) {
526 $commentAttributes =
$comment->attributes();
528 if ($commentAttributes->Text) {
529 $objPHPExcel->getActiveSheet()->getComment( (
string)$commentAttributes->ObjectBound )
530 ->setAuthor( (
string)$commentAttributes->Author )
531 ->setText($this->_parseRichText((
string)$commentAttributes->Text) );
537 foreach($sheet->Styles->StyleRegion as $styleRegion) {
538 $styleAttributes = $styleRegion->attributes();
542 if (($styleAttributes[
'startRow'] <= $maxRow) &&
543 ($styleAttributes[
'startCol'] <= $maxCol)) {
546 $startRow = $styleAttributes[
'startRow'] + 1;
548 $endColumn = ($styleAttributes[
'endCol'] > $maxCol) ? $maxCol : $styleAttributes[
'endCol'];
550 $endRow = ($styleAttributes[
'endRow'] > $maxRow) ? $maxRow : $styleAttributes[
'endRow'];
552 $cellRange = $startColumn.$startRow.
':'.$endColumn.$endRow;
555 $styleAttributes = $styleRegion->Style->attributes();
560 if ((!$this->_readDataOnly) ||
562 $styleArray = array();
563 $styleArray[
'numberformat'][
'code'] = (string) $styleAttributes[
'Format'];
565 if (!$this->_readDataOnly) {
566 switch($styleAttributes[
'HAlign']) {
588 switch($styleAttributes[
'VAlign']) {
603 $styleArray[
'alignment'][
'wrap'] = ($styleAttributes[
'WrapText'] ==
'1') ? True : False;
604 $styleArray[
'alignment'][
'shrinkToFit'] = ($styleAttributes[
'ShrinkToFit'] ==
'1') ? True : False;
605 $styleArray[
'alignment'][
'indent'] = (intval($styleAttributes[
"Indent"]) > 0) ? $styleAttributes[
"indent"] : 0;
607 $RGB = self::_parseGnumericColour($styleAttributes[
"Fore"]);
608 $styleArray[
'font'][
'color'][
'rgb'] = $RGB;
609 $RGB = self::_parseGnumericColour($styleAttributes[
"Back"]);
610 $shade = $styleAttributes[
"Shade"];
611 if (($RGB !=
'000000') || ($shade !=
'0')) {
612 $styleArray[
'fill'][
'color'][
'rgb'] = $styleArray[
'fill'][
'startcolor'][
'rgb'] = $RGB;
613 $RGB2 = self::_parseGnumericColour($styleAttributes[
"PatternColor"]);
614 $styleArray[
'fill'][
'endcolor'][
'rgb'] = $RGB2;
679 $fontAttributes = $styleRegion->Style->Font->attributes();
682 $styleArray[
'font'][
'name'] = (string) $styleRegion->Style->Font;
683 $styleArray[
'font'][
'size'] = intval($fontAttributes[
'Unit']);
684 $styleArray[
'font'][
'bold'] = ($fontAttributes[
'Bold'] ==
'1') ? True : False;
685 $styleArray[
'font'][
'italic'] = ($fontAttributes[
'Italic'] ==
'1') ? True : False;
686 $styleArray[
'font'][
'strike'] = ($fontAttributes[
'StrikeThrough'] ==
'1') ? True : False;
687 switch($fontAttributes[
'Underline']) {
704 switch($fontAttributes[
'Script']) {
706 $styleArray[
'font'][
'superScript'] = True;
709 $styleArray[
'font'][
'subScript'] = True;
713 if (isset($styleRegion->Style->StyleBorder)) {
714 if (isset($styleRegion->Style->StyleBorder->Top)) {
715 $styleArray[
'borders'][
'top'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Top->attributes());
717 if (isset($styleRegion->Style->StyleBorder->Bottom)) {
718 $styleArray[
'borders'][
'bottom'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Bottom->attributes());
720 if (isset($styleRegion->Style->StyleBorder->Left)) {
721 $styleArray[
'borders'][
'left'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Left->attributes());
723 if (isset($styleRegion->Style->StyleBorder->Right)) {
724 $styleArray[
'borders'][
'right'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Right->attributes());
726 if ((isset($styleRegion->Style->StyleBorder->Diagonal)) && (isset($styleRegion->Style->StyleBorder->{
'Rev-Diagonal'}))) {
727 $styleArray[
'borders'][
'diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
729 } elseif (isset($styleRegion->Style->StyleBorder->Diagonal)) {
730 $styleArray[
'borders'][
'diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
732 } elseif (isset($styleRegion->Style->StyleBorder->{
'Rev-Diagonal'})) {
733 $styleArray[
'borders'][
'diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->{
'Rev-Diagonal'}->attributes());
737 if (isset($styleRegion->Style->HyperLink)) {
739 $hyperlink = $styleRegion->Style->HyperLink->attributes();
744 $objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($styleArray);
749 if ((!$this->_readDataOnly) && (isset($sheet->Cols))) {
751 $columnAttributes = $sheet->Cols->attributes();
752 $defaultWidth = $columnAttributes[
'DefaultSizePts'] / 5.4;
754 foreach($sheet->Cols->ColInfo as $columnOverride) {
755 $columnAttributes = $columnOverride->attributes();
756 $column = $columnAttributes[
'No'];
757 $columnWidth = $columnAttributes[
'Unit'] / 5.4;
758 $hidden = ((isset($columnAttributes[
'Hidden'])) && ($columnAttributes[
'Hidden'] ==
'1')) ?
true :
false;
759 $columnCount = (isset($columnAttributes[
'Count'])) ? $columnAttributes[
'Count'] : 1;
760 while ($c < $column) {
764 while (($c < ($column+$columnCount)) && ($c <= $maxCol)) {
772 while ($c <= $maxCol) {
778 if ((!$this->_readDataOnly) && (isset($sheet->Rows))) {
780 $rowAttributes = $sheet->Rows->attributes();
781 $defaultHeight = $rowAttributes[
'DefaultSizePts'];
784 foreach($sheet->Rows->RowInfo as $rowOverride) {
785 $rowAttributes = $rowOverride->attributes();
786 $row = $rowAttributes[
'No'];
787 $rowHeight = $rowAttributes[
'Unit'];
788 $hidden = ((isset($rowAttributes[
'Hidden'])) && ($rowAttributes[
'Hidden'] ==
'1')) ?
true :
false;
789 $rowCount = (isset($rowAttributes[
'Count'])) ? $rowAttributes[
'Count'] : 1;
792 $objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
794 while (($r < (
$row+$rowCount)) && ($r < $maxRow)) {
796 $objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($rowHeight);
798 $objPHPExcel->getActiveSheet()->getRowDimension($r)->setVisible(
false);
802 while ($r < $maxRow) {
804 $objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
809 if (isset($sheet->MergedRegions)) {
810 foreach($sheet->MergedRegions->Merge as $mergeCells) {
811 $objPHPExcel->getActiveSheet()->mergeCells($mergeCells);
819 if (isset($gnmXML->Names)) {
820 foreach($gnmXML->Names->Name as $namedRange) {
821 $name = (string) $namedRange->name;
822 $range = (
string) $namedRange->value;
823 if (stripos($range,
'#REF!') !==
false) {
827 $range = explode(
'!',$range);
828 $range[0] = trim($range[0],
"'");;
829 if ($worksheet = $objPHPExcel->getSheetByName($range[0])) {
830 $extractedRange = str_replace(
'$',
'', $range[1]);
842 $styleArray = array();
844 if (isset($borderAttributes[
"Color"])) {
845 $RGB = self::_parseGnumericColour($borderAttributes[
"Color"]);
846 $styleArray[
'color'][
'rgb'] = $RGB;
849 switch ($borderAttributes[
"Style"]) {
899 $value->createText($is);
905 list($gnmR,$gnmG,$gnmB) = explode(
':',$gnmColour);
906 $gnmR = substr(str_pad($gnmR,4,
'0',STR_PAD_RIGHT),0,2);
907 $gnmG = substr(str_pad($gnmG,4,
'0',STR_PAD_RIGHT),0,2);
908 $gnmB = substr(str_pad($gnmB,4,
'0',STR_PAD_RIGHT),0,2);
909 $RGB = $gnmR.$gnmG.$gnmB;