OOCalc.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Reader
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. /**
  36. * PHPExcel_Reader_OOCalc
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Reader
  40. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Reader_OOCalc extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  43. {
  44. /**
  45. * Formats
  46. *
  47. * @var array
  48. */
  49. private $_styles = array();
  50. /**
  51. * Create a new PHPExcel_Reader_OOCalc
  52. */
  53. public function __construct() {
  54. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  55. }
  56. /**
  57. * Can the current PHPExcel_Reader_IReader read the file?
  58. *
  59. * @param string $pFilename
  60. * @return boolean
  61. * @throws PHPExcel_Reader_Exception
  62. */
  63. public function canRead($pFilename)
  64. {
  65. // Check if file exists
  66. if (!file_exists($pFilename)) {
  67. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  68. }
  69. // Check if zip class exists
  70. if (!class_exists('ZipArchive',FALSE)) {
  71. throw new PHPExcel_Reader_Exception("ZipArchive library is not enabled");
  72. }
  73. $mimeType = 'UNKNOWN';
  74. // Load file
  75. $zip = new ZipArchive;
  76. if ($zip->open($pFilename) === true) {
  77. // check if it is an OOXML archive
  78. $stat = $zip->statName('mimetype');
  79. if ($stat && ($stat['size'] <= 255)) {
  80. $mimeType = $zip->getFromName($stat['name']);
  81. } elseif($stat = $zip->statName('META-INF/manifest.xml')) {
  82. $xml = simplexml_load_string($zip->getFromName('META-INF/manifest.xml'));
  83. $namespacesContent = $xml->getNamespaces(true);
  84. if (isset($namespacesContent['manifest'])) {
  85. $manifest = $xml->children($namespacesContent['manifest']);
  86. foreach($manifest as $manifestDataSet) {
  87. $manifestAttributes = $manifestDataSet->attributes($namespacesContent['manifest']);
  88. if ($manifestAttributes->{'full-path'} == '/') {
  89. $mimeType = (string) $manifestAttributes->{'media-type'};
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. $zip->close();
  96. return ($mimeType === 'application/vnd.oasis.opendocument.spreadsheet');
  97. }
  98. return FALSE;
  99. }
  100. /**
  101. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  102. *
  103. * @param string $pFilename
  104. * @throws PHPExcel_Reader_Exception
  105. */
  106. public function listWorksheetNames($pFilename)
  107. {
  108. // Check if file exists
  109. if (!file_exists($pFilename)) {
  110. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  111. }
  112. $zip = new ZipArchive;
  113. if (!$zip->open($pFilename)) {
  114. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
  115. }
  116. $worksheetNames = array();
  117. $xml = new XMLReader();
  118. $res = $xml->open('zip://'.realpath($pFilename).'#content.xml');
  119. $xml->setParserProperty(2,true);
  120. // Step into the first level of content of the XML
  121. $xml->read();
  122. while ($xml->read()) {
  123. // Quickly jump through to the office:body node
  124. while ($xml->name !== 'office:body') {
  125. if ($xml->isEmptyElement)
  126. $xml->read();
  127. else
  128. $xml->next();
  129. }
  130. // Now read each node until we find our first table:table node
  131. while ($xml->read()) {
  132. if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
  133. // Loop through each table:table node reading the table:name attribute for each worksheet name
  134. do {
  135. $worksheetNames[] = $xml->getAttribute('table:name');
  136. $xml->next();
  137. } while ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT);
  138. }
  139. }
  140. }
  141. return $worksheetNames;
  142. }
  143. /**
  144. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  145. *
  146. * @param string $pFilename
  147. * @throws PHPExcel_Reader_Exception
  148. */
  149. public function listWorksheetInfo($pFilename)
  150. {
  151. // Check if file exists
  152. if (!file_exists($pFilename)) {
  153. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  154. }
  155. $worksheetInfo = array();
  156. $zip = new ZipArchive;
  157. if (!$zip->open($pFilename)) {
  158. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
  159. }
  160. $xml = new XMLReader();
  161. $res = $xml->open('zip://'.realpath($pFilename).'#content.xml');
  162. $xml->setParserProperty(2,true);
  163. // Step into the first level of content of the XML
  164. $xml->read();
  165. while ($xml->read()) {
  166. // Quickly jump through to the office:body node
  167. while ($xml->name !== 'office:body') {
  168. if ($xml->isEmptyElement)
  169. $xml->read();
  170. else
  171. $xml->next();
  172. }
  173. // Now read each node until we find our first table:table node
  174. while ($xml->read()) {
  175. if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
  176. $worksheetNames[] = $xml->getAttribute('table:name');
  177. $tmpInfo = array(
  178. 'worksheetName' => $xml->getAttribute('table:name'),
  179. 'lastColumnLetter' => 'A',
  180. 'lastColumnIndex' => 0,
  181. 'totalRows' => 0,
  182. 'totalColumns' => 0,
  183. );
  184. // Loop through each child node of the table:table element reading
  185. $currCells = 0;
  186. do {
  187. $xml->read();
  188. if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
  189. $rowspan = $xml->getAttribute('table:number-rows-repeated');
  190. $rowspan = empty($rowspan) ? 1 : $rowspan;
  191. $tmpInfo['totalRows'] += $rowspan;
  192. $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'],$currCells);
  193. $currCells = 0;
  194. // Step into the row
  195. $xml->read();
  196. do {
  197. if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
  198. if (!$xml->isEmptyElement) {
  199. $currCells++;
  200. $xml->next();
  201. } else {
  202. $xml->read();
  203. }
  204. } elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
  205. $mergeSize = $xml->getAttribute('table:number-columns-repeated');
  206. $currCells += $mergeSize;
  207. $xml->read();
  208. }
  209. } while ($xml->name != 'table:table-row');
  210. }
  211. } while ($xml->name != 'table:table');
  212. $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'],$currCells);
  213. $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
  214. $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
  215. $worksheetInfo[] = $tmpInfo;
  216. }
  217. }
  218. // foreach($workbookData->table as $worksheetDataSet) {
  219. // $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
  220. // $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
  221. //
  222. // $rowIndex = 0;
  223. // foreach ($worksheetData as $key => $rowData) {
  224. // switch ($key) {
  225. // case 'table-row' :
  226. // $rowDataTableAttributes = $rowData->attributes($namespacesContent['table']);
  227. // $rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ?
  228. // $rowDataTableAttributes['number-rows-repeated'] : 1;
  229. // $columnIndex = 0;
  230. //
  231. // foreach ($rowData as $key => $cellData) {
  232. // $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
  233. // $colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ?
  234. // $cellDataTableAttributes['number-columns-repeated'] : 1;
  235. // $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
  236. // if (isset($cellDataOfficeAttributes['value-type'])) {
  237. // $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex + $colRepeats - 1);
  238. // $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex + $rowRepeats);
  239. // }
  240. // $columnIndex += $colRepeats;
  241. // }
  242. // $rowIndex += $rowRepeats;
  243. // break;
  244. // }
  245. // }
  246. //
  247. // $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
  248. // $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
  249. //
  250. // }
  251. // }
  252. }
  253. return $worksheetInfo;
  254. }
  255. /**
  256. * Loads PHPExcel from file
  257. *
  258. * @param string $pFilename
  259. * @return PHPExcel
  260. * @throws PHPExcel_Reader_Exception
  261. */
  262. public function load($pFilename)
  263. {
  264. // Create new PHPExcel
  265. $objPHPExcel = new PHPExcel();
  266. // Load into this instance
  267. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  268. }
  269. private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
  270. $styleAttributeValue = strtolower($styleAttributeValue);
  271. foreach($styleList as $style) {
  272. if ($styleAttributeValue == strtolower($style)) {
  273. $styleAttributeValue = $style;
  274. return true;
  275. }
  276. }
  277. return false;
  278. }
  279. /**
  280. * Loads PHPExcel from file into PHPExcel instance
  281. *
  282. * @param string $pFilename
  283. * @param PHPExcel $objPHPExcel
  284. * @return PHPExcel
  285. * @throws PHPExcel_Reader_Exception
  286. */
  287. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  288. {
  289. // Check if file exists
  290. if (!file_exists($pFilename)) {
  291. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  292. }
  293. $timezoneObj = new DateTimeZone('Europe/London');
  294. $GMT = new DateTimeZone('UTC');
  295. $zip = new ZipArchive;
  296. if (!$zip->open($pFilename)) {
  297. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
  298. }
  299. // echo '<h1>Meta Information</h1>';
  300. $xml = simplexml_load_string($zip->getFromName("meta.xml"));
  301. $namespacesMeta = $xml->getNamespaces(true);
  302. // echo '<pre>';
  303. // print_r($namespacesMeta);
  304. // echo '</pre><hr />';
  305. $docProps = $objPHPExcel->getProperties();
  306. $officeProperty = $xml->children($namespacesMeta['office']);
  307. foreach($officeProperty as $officePropertyData) {
  308. $officePropertyDC = array();
  309. if (isset($namespacesMeta['dc'])) {
  310. $officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
  311. }
  312. foreach($officePropertyDC as $propertyName => $propertyValue) {
  313. switch ($propertyName) {
  314. case 'title' :
  315. $docProps->setTitle($propertyValue);
  316. break;
  317. case 'subject' :
  318. $docProps->setSubject($propertyValue);
  319. break;
  320. case 'creator' :
  321. $docProps->setCreator($propertyValue);
  322. $docProps->setLastModifiedBy($propertyValue);
  323. break;
  324. case 'date' :
  325. $creationDate = strtotime($propertyValue);
  326. $docProps->setCreated($creationDate);
  327. $docProps->setModified($creationDate);
  328. break;
  329. case 'description' :
  330. $docProps->setDescription($propertyValue);
  331. break;
  332. }
  333. }
  334. $officePropertyMeta = array();
  335. if (isset($namespacesMeta['dc'])) {
  336. $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
  337. }
  338. foreach($officePropertyMeta as $propertyName => $propertyValue) {
  339. $propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
  340. switch ($propertyName) {
  341. case 'initial-creator' :
  342. $docProps->setCreator($propertyValue);
  343. break;
  344. case 'keyword' :
  345. $docProps->setKeywords($propertyValue);
  346. break;
  347. case 'creation-date' :
  348. $creationDate = strtotime($propertyValue);
  349. $docProps->setCreated($creationDate);
  350. break;
  351. case 'user-defined' :
  352. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
  353. foreach ($propertyValueAttributes as $key => $value) {
  354. if ($key == 'name') {
  355. $propertyValueName = (string) $value;
  356. } elseif($key == 'value-type') {
  357. switch ($value) {
  358. case 'date' :
  359. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'date');
  360. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_DATE;
  361. break;
  362. case 'boolean' :
  363. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'bool');
  364. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_BOOLEAN;
  365. break;
  366. case 'float' :
  367. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'r4');
  368. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_FLOAT;
  369. break;
  370. default :
  371. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
  372. }
  373. }
  374. }
  375. $docProps->setCustomProperty($propertyValueName,$propertyValue,$propertyValueType);
  376. break;
  377. }
  378. }
  379. }
  380. // echo '<h1>Workbook Content</h1>';
  381. $xml = simplexml_load_string($zip->getFromName("content.xml"));
  382. $namespacesContent = $xml->getNamespaces(true);
  383. // echo '<pre>';
  384. // print_r($namespacesContent);
  385. // echo '</pre><hr />';
  386. $workbook = $xml->children($namespacesContent['office']);
  387. foreach($workbook->body->spreadsheet as $workbookData) {
  388. $workbookData = $workbookData->children($namespacesContent['table']);
  389. $worksheetID = 0;
  390. foreach($workbookData->table as $worksheetDataSet) {
  391. $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
  392. // print_r($worksheetData);
  393. // echo '<br />';
  394. $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
  395. // print_r($worksheetDataAttributes);
  396. // echo '<br />';
  397. if ((isset($this->_loadSheetsOnly)) && (isset($worksheetDataAttributes['name'])) &&
  398. (!in_array($worksheetDataAttributes['name'], $this->_loadSheetsOnly))) {
  399. continue;
  400. }
  401. // echo '<h2>Worksheet '.$worksheetDataAttributes['name'].'</h2>';
  402. // Create new Worksheet
  403. $objPHPExcel->createSheet();
  404. $objPHPExcel->setActiveSheetIndex($worksheetID);
  405. if (isset($worksheetDataAttributes['name'])) {
  406. $worksheetName = (string) $worksheetDataAttributes['name'];
  407. // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
  408. // formula cells... during the load, all formulae should be correct, and we're simply
  409. // bringing the worksheet name in line with the formula, not the reverse
  410. $objPHPExcel->getActiveSheet()->setTitle($worksheetName,false);
  411. }
  412. $rowID = 1;
  413. foreach($worksheetData as $key => $rowData) {
  414. // echo '<b>'.$key.'</b><br />';
  415. switch ($key) {
  416. case 'table-header-rows':
  417. foreach ($rowData as $key=>$cellData) {
  418. $rowData = $cellData;
  419. break;
  420. }
  421. case 'table-row' :
  422. $rowDataTableAttributes = $rowData->attributes($namespacesContent['table']);
  423. $rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ?
  424. $rowDataTableAttributes['number-rows-repeated'] : 1;
  425. $columnID = 'A';
  426. foreach($rowData as $key => $cellData) {
  427. if ($this->getReadFilter() !== NULL) {
  428. if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
  429. continue;
  430. }
  431. }
  432. // echo '<b>'.$columnID.$rowID.'</b><br />';
  433. $cellDataText = (isset($namespacesContent['text'])) ?
  434. $cellData->children($namespacesContent['text']) :
  435. '';
  436. $cellDataOffice = $cellData->children($namespacesContent['office']);
  437. $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
  438. $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
  439. // echo 'Office Attributes: ';
  440. // print_r($cellDataOfficeAttributes);
  441. // echo '<br />Table Attributes: ';
  442. // print_r($cellDataTableAttributes);
  443. // echo '<br />Cell Data Text';
  444. // print_r($cellDataText);
  445. // echo '<br />';
  446. //
  447. $type = $formatting = $hyperlink = null;
  448. $hasCalculatedValue = false;
  449. $cellDataFormula = '';
  450. if (isset($cellDataTableAttributes['formula'])) {
  451. $cellDataFormula = $cellDataTableAttributes['formula'];
  452. $hasCalculatedValue = true;
  453. }
  454. if (isset($cellDataOffice->annotation)) {
  455. // echo 'Cell has comment<br />';
  456. $annotationText = $cellDataOffice->annotation->children($namespacesContent['text']);
  457. $textArray = array();
  458. foreach($annotationText as $t) {
  459. foreach($t->span as $text) {
  460. $textArray[] = (string)$text;
  461. }
  462. }
  463. $text = implode("\n",$textArray);
  464. // echo $text,'<br />';
  465. $objPHPExcel->getActiveSheet()->getComment( $columnID.$rowID )
  466. // ->setAuthor( $author )
  467. ->setText($this->_parseRichText($text) );
  468. }
  469. if (isset($cellDataText->p)) {
  470. // Consolidate if there are multiple p records (maybe with spans as well)
  471. $dataArray = array();
  472. // Text can have multiple text:p and within those, multiple text:span.
  473. // text:p newlines, but text:span does not.
  474. // Also, here we assume there is no text data is span fields are specified, since
  475. // we have no way of knowing proper positioning anyway.
  476. foreach ($cellDataText->p as $pData) {
  477. if (isset($pData->span)) {
  478. // span sections do not newline, so we just create one large string here
  479. $spanSection = "";
  480. foreach ($pData->span as $spanData) {
  481. $spanSection .= $spanData;
  482. }
  483. array_push($dataArray, $spanSection);
  484. } else {
  485. array_push($dataArray, $pData);
  486. }
  487. }
  488. $allCellDataText = implode($dataArray, "\n");
  489. // echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
  490. switch ($cellDataOfficeAttributes['value-type']) {
  491. case 'string' :
  492. $type = PHPExcel_Cell_DataType::TYPE_STRING;
  493. $dataValue = $allCellDataText;
  494. if (isset($dataValue->a)) {
  495. $dataValue = $dataValue->a;
  496. $cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']);
  497. $hyperlink = $cellXLinkAttributes['href'];
  498. }
  499. break;
  500. case 'boolean' :
  501. $type = PHPExcel_Cell_DataType::TYPE_BOOL;
  502. $dataValue = ($allCellDataText == 'TRUE') ? True : False;
  503. break;
  504. case 'percentage' :
  505. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  506. $dataValue = (float) $cellDataOfficeAttributes['value'];
  507. if (floor($dataValue) == $dataValue) {
  508. $dataValue = (integer) $dataValue;
  509. }
  510. $formatting = PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00;
  511. break;
  512. case 'currency' :
  513. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  514. $dataValue = (float) $cellDataOfficeAttributes['value'];
  515. if (floor($dataValue) == $dataValue) {
  516. $dataValue = (integer) $dataValue;
  517. }
  518. $formatting = PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
  519. break;
  520. case 'float' :
  521. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  522. $dataValue = (float) $cellDataOfficeAttributes['value'];
  523. if (floor($dataValue) == $dataValue) {
  524. if ($dataValue = (integer) $dataValue)
  525. $dataValue = (integer) $dataValue;
  526. else
  527. $dataValue = (float) $dataValue;
  528. }
  529. break;
  530. case 'date' :
  531. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  532. $dateObj = new DateTime($cellDataOfficeAttributes['date-value'], $GMT);
  533. $dateObj->setTimeZone($timezoneObj);
  534. list($year,$month,$day,$hour,$minute,$second) = explode(' ',$dateObj->format('Y m d H i s'));
  535. $dataValue = PHPExcel_Shared_Date::FormattedPHPToExcel($year,$month,$day,$hour,$minute,$second);
  536. if ($dataValue != floor($dataValue)) {
  537. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15.' '.PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  538. } else {
  539. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15;
  540. }
  541. break;
  542. case 'time' :
  543. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  544. $dataValue = PHPExcel_Shared_Date::PHPToExcel(strtotime('01-01-1970 '.implode(':',sscanf($cellDataOfficeAttributes['time-value'],'PT%dH%dM%dS'))));
  545. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  546. break;
  547. }
  548. // echo 'Data value is '.$dataValue.'<br />';
  549. // if ($hyperlink !== NULL) {
  550. // echo 'Hyperlink is '.$hyperlink.'<br />';
  551. // }
  552. } else {
  553. $type = PHPExcel_Cell_DataType::TYPE_NULL;
  554. $dataValue = NULL;
  555. }
  556. if ($hasCalculatedValue) {
  557. $type = PHPExcel_Cell_DataType::TYPE_FORMULA;
  558. // echo 'Formula: '.$cellDataFormula.'<br />';
  559. $cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,':=')+1);
  560. $temp = explode('"',$cellDataFormula);
  561. $tKey = false;
  562. foreach($temp as &$value) {
  563. // Only replace in alternate array entries (i.e. non-quoted blocks)
  564. if ($tKey = !$tKey) {
  565. $value = preg_replace('/\[\.(.*):\.(.*)\]/Ui','$1:$2',$value);
  566. $value = preg_replace('/\[\.(.*)\]/Ui','$1',$value);
  567. $value = PHPExcel_Calculation::_translateSeparator(';',',',$value,$inBraces);
  568. }
  569. }
  570. unset($value);
  571. // Then rebuild the formula string
  572. $cellDataFormula = implode('"',$temp);
  573. // echo 'Adjusted Formula: '.$cellDataFormula.'<br />';
  574. }
  575. $colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ?
  576. $cellDataTableAttributes['number-columns-repeated'] : 1;
  577. if ($type !== NULL) {
  578. for ($i = 0; $i < $colRepeats; ++$i) {
  579. if ($i > 0) {
  580. ++$columnID;
  581. }
  582. if ($type !== PHPExcel_Cell_DataType::TYPE_NULL) {
  583. for ($rowAdjust = 0; $rowAdjust < $rowRepeats; ++$rowAdjust) {
  584. $rID = $rowID + $rowAdjust;
  585. $objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $dataValue),$type);
  586. if ($hasCalculatedValue) {
  587. // echo 'Forumla result is '.$dataValue.'<br />';
  588. $objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->setCalculatedValue($dataValue);
  589. }
  590. if ($formatting !== NULL) {
  591. $objPHPExcel->getActiveSheet()->getStyle($columnID.$rID)->getNumberFormat()->setFormatCode($formatting);
  592. } else {
  593. $objPHPExcel->getActiveSheet()->getStyle($columnID.$rID)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
  594. }
  595. if ($hyperlink !== NULL) {
  596. $objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->getHyperlink()->setUrl($hyperlink);
  597. }
  598. }
  599. }
  600. }
  601. }
  602. // Merged cells
  603. if ((isset($cellDataTableAttributes['number-columns-spanned'])) || (isset($cellDataTableAttributes['number-rows-spanned']))) {
  604. if (($type !== PHPExcel_Cell_DataType::TYPE_NULL) || (!$this->_readDataOnly)) {
  605. $columnTo = $columnID;
  606. if (isset($cellDataTableAttributes['number-columns-spanned'])) {
  607. $columnTo = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-spanned'] -2);
  608. }
  609. $rowTo = $rowID;
  610. if (isset($cellDataTableAttributes['number-rows-spanned'])) {
  611. $rowTo = $rowTo + $cellDataTableAttributes['number-rows-spanned'] - 1;
  612. }
  613. $cellRange = $columnID.$rowID.':'.$columnTo.$rowTo;
  614. $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
  615. }
  616. }
  617. ++$columnID;
  618. }
  619. $rowID += $rowRepeats;
  620. break;
  621. }
  622. }
  623. ++$worksheetID;
  624. }
  625. }
  626. // Return
  627. return $objPHPExcel;
  628. }
  629. private function _parseRichText($is = '') {
  630. $value = new PHPExcel_RichText();
  631. $value->createText($is);
  632. return $value;
  633. }
  634. }