OOCalc.php 26 KB

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