30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
35 require(PHPEXCEL_ROOT .
'PHPExcel/Autoloader.php');
100 $this->_readDataOnly = $pValue;
127 $this->_loadSheetsOnly = is_array($value) ?
128 $value : array($value);
140 $this->_loadSheetsOnly = null;
160 $this->_readFilter = $pValue;
180 if (!class_exists(
'ZipArchive')) {
185 if (!file_exists($pFilename)) {
186 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
190 $zip =
new ZipArchive;
191 if ($zip->open($pFilename) ===
true) {
193 $mimeType = $zip->getFromName(
"mimetype");
197 return ($mimeType ===
'application/vnd.oasis.opendocument.spreadsheet');
212 if (!file_exists($pFilename)) {
213 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
216 $worksheetNames = array();
218 $zip =
new ZipArchive;
219 if ($zip->open($pFilename) ===
true) {
221 $xml = simplexml_load_string($zip->getFromName(
"content.xml"));
222 $namespacesContent = $xml->getNamespaces(
true);
224 $workbook = $xml->children($namespacesContent[
'office']);
225 foreach($workbook->body->spreadsheet as $workbookData) {
226 $workbookData = $workbookData->children($namespacesContent[
'table']);
227 foreach($workbookData->table as $worksheetDataSet) {
228 $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent[
'table']);
230 $worksheetNames[] = $worksheetDataAttributes[
'name'];
235 return $worksheetNames;
246 public function load($pFilename)
256 $styleAttributeValue = strtolower($styleAttributeValue);
257 foreach($styleList as $style) {
258 if ($styleAttributeValue == strtolower($style)) {
259 $styleAttributeValue = $style;
277 if (!file_exists($pFilename)) {
278 throw new Exception(
"Could not open " . $pFilename .
" for reading! File does not exist.");
281 $timezoneObj =
new DateTimeZone(
'Europe/London');
282 $GMT =
new DateTimeZone(
'UTC');
284 $zip =
new ZipArchive;
285 if ($zip->open($pFilename) ===
true) {
287 $xml = simplexml_load_string($zip->getFromName(
"meta.xml"));
288 $namespacesMeta = $xml->getNamespaces(
true);
294 $officeProperty = $xml->children($namespacesMeta[
'office']);
295 foreach($officeProperty as $officePropertyData) {
296 $officePropertyDC = array();
297 if (isset($namespacesMeta[
'dc'])) {
298 $officePropertyDC = $officePropertyData->children($namespacesMeta[
'dc']);
300 foreach($officePropertyDC as $propertyName => $propertyValue) {
301 switch ($propertyName) {
303 $docProps->setTitle($propertyValue);
306 $docProps->setSubject($propertyValue);
309 $docProps->setCreator($propertyValue);
310 $docProps->setLastModifiedBy($propertyValue);
313 $creationDate = strtotime($propertyValue);
314 $docProps->setCreated($creationDate);
315 $docProps->setModified($creationDate);
318 $docProps->setDescription($propertyValue);
322 $officePropertyMeta = array();
323 if (isset($namespacesMeta[
'dc'])) {
324 $officePropertyMeta = $officePropertyData->children($namespacesMeta[
'meta']);
326 foreach($officePropertyMeta as $propertyName => $propertyValue) {
327 $propertyValueAttributes = $propertyValue->attributes($namespacesMeta[
'meta']);
328 switch ($propertyName) {
329 case 'initial-creator' :
330 $docProps->setCreator($propertyValue);
333 $docProps->setKeywords($propertyValue);
335 case 'creation-date' :
336 $creationDate = strtotime($propertyValue);
337 $docProps->setCreated($creationDate);
339 case 'user-defined' :
341 foreach ($propertyValueAttributes as $key => $value) {
342 if ($key ==
'name') {
343 $propertyValueName = (string) $value;
344 } elseif($key ==
'value-type') {
363 $docProps->setCustomProperty($propertyValueName,$propertyValue,$propertyValueType);
371 $xml = simplexml_load_string($zip->getFromName(
"content.xml"));
372 $namespacesContent = $xml->getNamespaces(
true);
377 $workbook = $xml->children($namespacesContent[
'office']);
378 foreach($workbook->body->spreadsheet as $workbookData) {
379 $workbookData = $workbookData->children($namespacesContent[
'table']);
381 foreach($workbookData->table as $worksheetDataSet) {
382 $worksheetData = $worksheetDataSet->children($namespacesContent[
'table']);
385 $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent[
'table']);
388 if ((isset($this->_loadSheetsOnly)) && (isset($worksheetDataAttributes[
'name'])) &&
389 (!in_array($worksheetDataAttributes[
'name'], $this->_loadSheetsOnly))) {
397 if (isset($worksheetDataAttributes[
'name'])) {
398 $worksheetName = (string) $worksheetDataAttributes[
'name'];
403 foreach($worksheetData as $key => $rowData) {
406 case 'table-header-rows':
407 foreach ($rowData as $key=>$cellData) {
408 $rowData = $cellData;
413 foreach($rowData as $key => $cellData) {
415 if (!$this->
getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
421 $cellDataText = $cellData->children($namespacesContent[
'text']);
422 $cellDataOffice = $cellData->children($namespacesContent[
'office']);
423 $cellDataOfficeAttributes = $cellData->attributes($namespacesContent[
'office']);
424 $cellDataTableAttributes = $cellData->attributes($namespacesContent[
'table']);
434 $type = $formatting = $hyperlink = null;
435 $hasCalculatedValue =
false;
436 $cellDataFormula =
'';
437 if (isset($cellDataTableAttributes[
'formula'])) {
438 $cellDataFormula = $cellDataTableAttributes[
'formula'];
439 $hasCalculatedValue =
true;
442 if (isset($cellDataOffice->annotation)) {
444 $annotationText = $cellDataOffice->annotation->children($namespacesContent[
'text']);
445 $textArray = array();
446 foreach($annotationText as
$t) {
447 foreach($t->span as $text) {
448 $textArray[] = (string)$text;
451 $text = implode(
"\n",$textArray);
458 if (isset($cellDataText->p)) {
460 switch ($cellDataOfficeAttributes[
'value-type']) {
463 $dataValue = $cellDataText->p;
464 if (isset($dataValue->a)) {
465 $dataValue = $dataValue->a;
466 $cellXLinkAttributes = $dataValue->attributes($namespacesContent[
'xlink']);
467 $hyperlink = $cellXLinkAttributes[
'href'];
472 $dataValue = ($cellDataText->p ==
'TRUE') ? True : False;
476 $dataValue = (float) $cellDataOfficeAttributes[
'value'];
477 if (floor($dataValue) == $dataValue) {
478 $dataValue = (integer) $dataValue;
483 $dateObj =
new DateTime($cellDataOfficeAttributes[
'date-value'], $GMT);
484 $dateObj->setTimeZone($timezoneObj);
485 list($year,$month,$day,$hour,$minute,$second) = explode(
' ',$dateObj->format(
'Y m d H i s'));
487 if ($dataValue != floor($dataValue)) {
505 if ($hasCalculatedValue) {
508 $cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,
':=')+1);
509 $temp = explode(
'"',$cellDataFormula);
511 foreach($temp as &$value) {
513 if ($tKey = !$tKey) {
514 $value = preg_replace(
'/\[\.(.*):\.(.*)\]/Ui',
'$1:$2',$value);
515 $value = preg_replace(
'/\[\.(.*)\]/Ui',
'$1',$value);
521 $cellDataFormula = implode(
'"',$temp);
525 if (!is_null(
$type)) {
526 $objPHPExcel->
getActiveSheet()->getCell($columnID.$rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $dataValue),
$type);
527 if ($hasCalculatedValue) {
529 $objPHPExcel->
getActiveSheet()->getCell($columnID.$rowID)->setCalculatedValue($dataValue);
531 if (($cellDataOfficeAttributes[
'value-type'] ==
'date') ||
532 ($cellDataOfficeAttributes[
'value-type'] ==
'time')) {
533 $objPHPExcel->
getActiveSheet()->getStyle($columnID.$rowID)->getNumberFormat()->setFormatCode($formatting);
535 if (!is_null($hyperlink)) {
536 $objPHPExcel->
getActiveSheet()->getCell($columnID.$rowID)->getHyperlink()->setUrl($hyperlink);
541 if ((isset($cellDataTableAttributes[
'number-columns-spanned'])) || (isset($cellDataTableAttributes[
'number-rows-spanned']))) {
542 $columnTo = $columnID;
543 if (isset($cellDataTableAttributes[
'number-columns-spanned'])) {
547 if (isset($cellDataTableAttributes[
'number-rows-spanned'])) {
548 $rowTo = $rowTo + $cellDataTableAttributes[
'number-rows-spanned'] - 1;
550 $cellRange = $columnID.$rowID.
':'.$columnTo.$rowTo;
554 if (isset($cellDataTableAttributes[
'number-columns-repeated'])) {
577 $value->createText($is);