PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Reader/Excel2003XML.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 867 lines | 588 code | 69 blank | 210 comment | 83 complexity | 0be373a32a4db10a74457bbd98c7cb52 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /** PHPExcel root directory */
  28. if (! defined('PHPEXCEL_ROOT'))
  29. {
  30. /**
  31. * @ignore
  32. */
  33. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  34. require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  35. }
  36. /**
  37. * PHPExcel_Reader_Excel2003XML
  38. *
  39. * @category PHPExcel
  40. * @package PHPExcel_Reader
  41. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  42. */
  43. class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
  44. {
  45. /**
  46. * Read data only?
  47. *
  48. * @var boolean
  49. */
  50. private $_readDataOnly = false;
  51. /**
  52. * Restict which sheets should be loaded?
  53. *
  54. * @var array
  55. */
  56. private $_loadSheetsOnly = null;
  57. /**
  58. * Formats
  59. *
  60. * @var array
  61. */
  62. private $_styles = array();
  63. /**
  64. * PHPExcel_Reader_IReadFilter instance
  65. *
  66. * @var PHPExcel_Reader_IReadFilter
  67. */
  68. private $_readFilter = null;
  69. /**
  70. * Read data only?
  71. *
  72. * @return boolean
  73. */
  74. public function getReadDataOnly()
  75. {
  76. return $this->_readDataOnly;
  77. }
  78. /**
  79. * Set read data only
  80. *
  81. * @param boolean $pValue
  82. * @return PHPExcel_Reader_Excel2003XML
  83. */
  84. public function setReadDataOnly($pValue = false)
  85. {
  86. $this->_readDataOnly = $pValue;
  87. return $this;
  88. }
  89. /**
  90. * Get which sheets to load
  91. *
  92. * @return mixed
  93. */
  94. public function getLoadSheetsOnly()
  95. {
  96. return $this->_loadSheetsOnly;
  97. }
  98. /**
  99. * Set which sheets to load
  100. *
  101. * @param mixed $value
  102. * @return PHPExcel_Reader_Excel2003XML
  103. */
  104. public function setLoadSheetsOnly($value = null)
  105. {
  106. $this->_loadSheetsOnly = is_array($value) ? $value : array($value);
  107. return $this;
  108. }
  109. /**
  110. * Set all sheets to load
  111. *
  112. * @return PHPExcel_Reader_Excel2003XML
  113. */
  114. public function setLoadAllSheets()
  115. {
  116. $this->_loadSheetsOnly = null;
  117. return $this;
  118. }
  119. /**
  120. * Read filter
  121. *
  122. * @return PHPExcel_Reader_IReadFilter
  123. */
  124. public function getReadFilter()
  125. {
  126. return $this->_readFilter;
  127. }
  128. /**
  129. * Set read filter
  130. *
  131. * @param PHPExcel_Reader_IReadFilter $pValue
  132. * @return PHPExcel_Reader_Excel2003XML
  133. */
  134. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue)
  135. {
  136. $this->_readFilter = $pValue;
  137. return $this;
  138. }
  139. /**
  140. * Create a new PHPExcel_Reader_Excel2003XML
  141. */
  142. public function __construct()
  143. {
  144. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  145. }
  146. /**
  147. * Can the current PHPExcel_Reader_IReader read the file?
  148. *
  149. * @param string $pFileName
  150. * @return boolean
  151. */
  152. public function canRead($pFilename)
  153. {
  154. // Office xmlns:o="urn:schemas-microsoft-com:office:office"
  155. // Excel xmlns:x="urn:schemas-microsoft-com:office:excel"
  156. // XML Spreadsheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  157. // Spreadsheet component xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet"
  158. // XML schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
  159. // XML data type xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
  160. // MS-persist recordset xmlns:rs="urn:schemas-microsoft-com:rowset"
  161. // Rowset xmlns:z="#RowsetSchema"
  162. //
  163. $signature = array('<?xml version="1.0"', '<?mso-application progid="Excel.Sheet"?>');
  164. // Check if file exists
  165. if (! file_exists($pFilename))
  166. {
  167. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  168. }
  169. // Read sample data (first 2 KB will do)
  170. $fh = fopen($pFilename, 'r');
  171. $data = fread($fh, 2048);
  172. fclose($fh);
  173. $valid = true;
  174. foreach ($signature as $match)
  175. {
  176. // every part of the signature must be present
  177. if (strpos($data, $match) === false)
  178. {
  179. $valid = false;
  180. break;
  181. }
  182. }
  183. return $valid;
  184. }
  185. /**
  186. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  187. *
  188. * @param string $pFilename
  189. * @throws Exception
  190. */
  191. public function listWorksheetNames($pFilename)
  192. {
  193. // Check if file exists
  194. if (! file_exists($pFilename))
  195. {
  196. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  197. }
  198. $worksheetNames = array();
  199. $xml = simplexml_load_file($pFilename);
  200. $namespaces = $xml->getNamespaces(true);
  201. $xml_ss = $xml->children($namespaces['ss']);
  202. foreach ($xml_ss->Worksheet as $worksheet)
  203. {
  204. $worksheet_ss = $worksheet->attributes($namespaces['ss']);
  205. $worksheetNames[] = $worksheet_ss['Name'];
  206. }
  207. return $worksheetNames;
  208. }
  209. /**
  210. * Loads PHPExcel from file
  211. *
  212. * @param string $pFilename
  213. * @return PHPExcel
  214. * @throws Exception
  215. */
  216. public function load($pFilename)
  217. {
  218. // Create new PHPExcel
  219. $objPHPExcel = new PHPExcel();
  220. // Load into this instance
  221. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  222. }
  223. private static function identifyFixedStyleValue($styleList, &$styleAttributeValue)
  224. {
  225. $styleAttributeValue = strtolower($styleAttributeValue);
  226. foreach ($styleList as $style)
  227. {
  228. if ($styleAttributeValue == strtolower($style))
  229. {
  230. $styleAttributeValue = $style;
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. /**
  237. * pixel units to excel width units(units of 1/256th of a character width)
  238. * @param pxs
  239. * @return
  240. */
  241. private static function _pixel2WidthUnits($pxs)
  242. {
  243. $UNIT_OFFSET_MAP = array(0, 36, 73, 109, 146, 182, 219);
  244. $widthUnits = 256 * ($pxs / 7);
  245. $widthUnits += $UNIT_OFFSET_MAP[($pxs % 7)];
  246. return $widthUnits;
  247. }
  248. /**
  249. * excel width units(units of 1/256th of a character width) to pixel units
  250. * @param widthUnits
  251. * @return
  252. */
  253. private static function _widthUnits2Pixel($widthUnits)
  254. {
  255. $pixels = ($widthUnits / 256) * 7;
  256. $offsetWidthUnits = $widthUnits % 256;
  257. $pixels += round($offsetWidthUnits / (256 / 7));
  258. return $pixels;
  259. }
  260. private static function _hex2str($hex)
  261. {
  262. return chr(hexdec($hex[1]));
  263. }
  264. /**
  265. * Loads PHPExcel from file into PHPExcel instance
  266. *
  267. * @param string $pFilename
  268. * @param PHPExcel $objPHPExcel
  269. * @return PHPExcel
  270. * @throws Exception
  271. */
  272. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  273. {
  274. $fromFormats = array('\-', '\ ');
  275. $toFormats = array('-', ' ');
  276. $underlineStyles = array(PHPExcel_Style_Font :: UNDERLINE_NONE, PHPExcel_Style_Font :: UNDERLINE_DOUBLE,
  277. PHPExcel_Style_Font :: UNDERLINE_DOUBLEACCOUNTING, PHPExcel_Style_Font :: UNDERLINE_SINGLE,
  278. PHPExcel_Style_Font :: UNDERLINE_SINGLEACCOUNTING);
  279. $verticalAlignmentStyles = array(PHPExcel_Style_Alignment :: VERTICAL_BOTTOM,
  280. PHPExcel_Style_Alignment :: VERTICAL_TOP, PHPExcel_Style_Alignment :: VERTICAL_CENTER,
  281. PHPExcel_Style_Alignment :: VERTICAL_JUSTIFY);
  282. $horizontalAlignmentStyles = array(PHPExcel_Style_Alignment :: HORIZONTAL_GENERAL,
  283. PHPExcel_Style_Alignment :: HORIZONTAL_LEFT, PHPExcel_Style_Alignment :: HORIZONTAL_RIGHT,
  284. PHPExcel_Style_Alignment :: HORIZONTAL_CENTER, PHPExcel_Style_Alignment :: HORIZONTAL_CENTER_CONTINUOUS,
  285. PHPExcel_Style_Alignment :: HORIZONTAL_JUSTIFY);
  286. $timezoneObj = new DateTimeZone('Europe/London');
  287. $GMT = new DateTimeZone('UTC');
  288. // Check if file exists
  289. if (! file_exists($pFilename))
  290. {
  291. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  292. }
  293. $xml = simplexml_load_file($pFilename);
  294. $namespaces = $xml->getNamespaces(true);
  295. $docProps = $objPHPExcel->getProperties();
  296. if (isset($xml->DocumentProperties[0]))
  297. {
  298. foreach ($xml->DocumentProperties[0] as $propertyName => $propertyValue)
  299. {
  300. switch ($propertyName)
  301. {
  302. case 'Title' :
  303. $docProps->setTitle($propertyValue);
  304. break;
  305. case 'Subject' :
  306. $docProps->setSubject($propertyValue);
  307. break;
  308. case 'Author' :
  309. $docProps->setCreator($propertyValue);
  310. break;
  311. case 'Created' :
  312. $creationDate = strtotime($propertyValue);
  313. $docProps->setCreated($creationDate);
  314. break;
  315. case 'LastAuthor' :
  316. $docProps->setLastModifiedBy($propertyValue);
  317. break;
  318. case 'LastSaved' :
  319. $lastSaveDate = strtotime($propertyValue);
  320. $docProps->setModified($lastSaveDate);
  321. break;
  322. case 'Company' :
  323. $docProps->setCompany($propertyValue);
  324. break;
  325. case 'Category' :
  326. $docProps->setCategory($propertyValue);
  327. break;
  328. case 'Manager' :
  329. $docProps->setManager($propertyValue);
  330. break;
  331. case 'Keywords' :
  332. $docProps->setKeywords($propertyValue);
  333. break;
  334. case 'Description' :
  335. $docProps->setDescription($propertyValue);
  336. break;
  337. }
  338. }
  339. }
  340. if (isset($xml->CustomDocumentProperties))
  341. {
  342. foreach ($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue)
  343. {
  344. $propertyAttributes = $propertyValue->attributes($namespaces['dt']);
  345. $propertyName = preg_replace_callback('/_x([0-9a-z]{4})_/', 'PHPExcel_Reader_Excel2003XML::_hex2str', $propertyName);
  346. $propertyType = PHPExcel_DocumentProperties :: PROPERTY_TYPE_UNKNOWN;
  347. switch ((string) $propertyAttributes)
  348. {
  349. case 'string' :
  350. $propertyType = PHPExcel_DocumentProperties :: PROPERTY_TYPE_STRING;
  351. $propertyValue = trim($propertyValue);
  352. break;
  353. case 'boolean' :
  354. $propertyType = PHPExcel_DocumentProperties :: PROPERTY_TYPE_BOOLEAN;
  355. $propertyValue = (bool) $propertyValue;
  356. break;
  357. case 'integer' :
  358. $propertyType = PHPExcel_DocumentProperties :: PROPERTY_TYPE_INTEGER;
  359. $propertyValue = intval($propertyValue);
  360. break;
  361. case 'float' :
  362. $propertyType = PHPExcel_DocumentProperties :: PROPERTY_TYPE_FLOAT;
  363. $propertyValue = floatval($propertyValue);
  364. break;
  365. case 'dateTime.tz' :
  366. $propertyType = PHPExcel_DocumentProperties :: PROPERTY_TYPE_DATE;
  367. $propertyValue = strtotime(trim($propertyValue));
  368. break;
  369. }
  370. $docProps->setCustomProperty($propertyName, $propertyValue, $propertyType);
  371. }
  372. }
  373. foreach ($xml->Styles[0] as $style)
  374. {
  375. $style_ss = $style->attributes($namespaces['ss']);
  376. $styleID = (string) $style_ss['ID'];
  377. // echo 'Style ID = '.$styleID.'<br />';
  378. if ($styleID == 'Default')
  379. {
  380. $this->_styles['Default'] = array();
  381. }
  382. else
  383. {
  384. $this->_styles[$styleID] = $this->_styles['Default'];
  385. }
  386. foreach ($style as $styleType => $styleData)
  387. {
  388. $styleAttributes = $styleData->attributes($namespaces['ss']);
  389. // echo $styleType.'<br />';
  390. switch ($styleType)
  391. {
  392. case 'Alignment' :
  393. foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue)
  394. {
  395. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  396. $styleAttributeValue = (string) $styleAttributeValue;
  397. switch ($styleAttributeKey)
  398. {
  399. case 'Vertical' :
  400. if (self :: identifyFixedStyleValue($verticalAlignmentStyles, $styleAttributeValue))
  401. {
  402. $this->_styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
  403. }
  404. break;
  405. case 'Horizontal' :
  406. if (self :: identifyFixedStyleValue($horizontalAlignmentStyles, $styleAttributeValue))
  407. {
  408. $this->_styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
  409. }
  410. break;
  411. case 'WrapText' :
  412. $this->_styles[$styleID]['alignment']['wrap'] = true;
  413. break;
  414. }
  415. }
  416. break;
  417. case 'Borders' :
  418. foreach ($styleData->Border as $borderStyle)
  419. {
  420. $borderAttributes = $borderStyle->attributes($namespaces['ss']);
  421. $thisBorder = array();
  422. foreach ($borderAttributes as $borderStyleKey => $borderStyleValue)
  423. {
  424. // echo $borderStyleKey.' = '.$borderStyleValue.'<br />';
  425. switch ($borderStyleKey)
  426. {
  427. case 'LineStyle' :
  428. $thisBorder['style'] = PHPExcel_Style_Border :: BORDER_MEDIUM;
  429. // $thisBorder['style'] = $borderStyleValue;
  430. break;
  431. case 'Weight' :
  432. // $thisBorder['style'] = $borderStyleValue;
  433. break;
  434. case 'Position' :
  435. $borderPosition = strtolower($borderStyleValue);
  436. break;
  437. case 'Color' :
  438. $borderColour = substr($borderStyleValue, 1);
  439. $thisBorder['color']['rgb'] = $borderColour;
  440. break;
  441. }
  442. }
  443. if (count($thisBorder) > 0)
  444. {
  445. if (($borderPosition == 'left') || ($borderPosition == 'right') || ($borderPosition == 'top') || ($borderPosition == 'bottom'))
  446. {
  447. $this->_styles[$styleID]['borders'][$borderPosition] = $thisBorder;
  448. }
  449. }
  450. }
  451. break;
  452. case 'Font' :
  453. foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue)
  454. {
  455. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  456. $styleAttributeValue = (string) $styleAttributeValue;
  457. switch ($styleAttributeKey)
  458. {
  459. case 'FontName' :
  460. $this->_styles[$styleID]['font']['name'] = $styleAttributeValue;
  461. break;
  462. case 'Size' :
  463. $this->_styles[$styleID]['font']['size'] = $styleAttributeValue;
  464. break;
  465. case 'Color' :
  466. $this->_styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue, 1);
  467. break;
  468. case 'Bold' :
  469. $this->_styles[$styleID]['font']['bold'] = true;
  470. break;
  471. case 'Italic' :
  472. $this->_styles[$styleID]['font']['italic'] = true;
  473. break;
  474. case 'Underline' :
  475. if (self :: identifyFixedStyleValue($underlineStyles, $styleAttributeValue))
  476. {
  477. $this->_styles[$styleID]['font']['underline'] = $styleAttributeValue;
  478. }
  479. break;
  480. }
  481. }
  482. break;
  483. case 'Interior' :
  484. foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue)
  485. {
  486. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  487. switch ($styleAttributeKey)
  488. {
  489. case 'Color' :
  490. $this->_styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue, 1);
  491. break;
  492. }
  493. }
  494. break;
  495. case 'NumberFormat' :
  496. foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue)
  497. {
  498. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  499. $styleAttributeValue = str_replace($fromFormats, $toFormats, $styleAttributeValue);
  500. switch ($styleAttributeValue)
  501. {
  502. case 'Short Date' :
  503. $styleAttributeValue = 'dd/mm/yyyy';
  504. break;
  505. }
  506. if ($styleAttributeValue > '')
  507. {
  508. $this->_styles[$styleID]['numberformat']['code'] = $styleAttributeValue;
  509. }
  510. }
  511. break;
  512. case 'Protection' :
  513. foreach ($styleAttributes as $styleAttributeKey => $styleAttributeValue)
  514. {
  515. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  516. }
  517. break;
  518. }
  519. }
  520. // print_r($this->_styles[$styleID]);
  521. // echo '<hr />';
  522. }
  523. // echo '<hr />';
  524. $worksheetID = 0;
  525. $xml_ss = $xml->children($namespaces['ss']);
  526. foreach ($xml_ss->Worksheet as $worksheet)
  527. {
  528. $worksheet_ss = $worksheet->attributes($namespaces['ss']);
  529. if ((isset($this->_loadSheetsOnly)) && (isset($worksheet_ss['Name'])) && (! in_array($worksheet_ss['Name'], $this->_loadSheetsOnly)))
  530. {
  531. continue;
  532. }
  533. // echo '<h3>Worksheet: ',$worksheet_ss['Name'],'<h3>';
  534. //
  535. // Create new Worksheet
  536. $objPHPExcel->createSheet();
  537. $objPHPExcel->setActiveSheetIndex($worksheetID);
  538. if (isset($worksheet_ss['Name']))
  539. {
  540. $worksheetName = (string) $worksheet_ss['Name'];
  541. $objPHPExcel->getActiveSheet()->setTitle($worksheetName);
  542. }
  543. $columnID = 'A';
  544. if (isset($worksheet->Table->Column))
  545. {
  546. foreach ($worksheet->Table->Column as $columnData)
  547. {
  548. $columnData_ss = $columnData->attributes($namespaces['ss']);
  549. if (isset($columnData_ss['Index']))
  550. {
  551. $columnID = PHPExcel_Cell :: stringFromColumnIndex($columnData_ss['Index'] - 1);
  552. }
  553. if (isset($columnData_ss['Width']))
  554. {
  555. $columnWidth = $columnData_ss['Width'];
  556. // echo '<b>Setting column width for '.$columnID.' to '.$columnWidth.'</b><br />';
  557. $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
  558. }
  559. ++ $columnID;
  560. }
  561. }
  562. $rowID = 1;
  563. if (isset($worksheet->Table->Row))
  564. {
  565. foreach ($worksheet->Table->Row as $rowData)
  566. {
  567. $rowHasData = false;
  568. $row_ss = $rowData->attributes($namespaces['ss']);
  569. if (isset($row_ss['Index']))
  570. {
  571. $rowID = (integer) $row_ss['Index'];
  572. }
  573. // echo '<b>Row '.$rowID.'</b><br />';
  574. $columnID = 'A';
  575. foreach ($rowData->Cell as $cell)
  576. {
  577. $cell_ss = $cell->attributes($namespaces['ss']);
  578. if (isset($cell_ss['Index']))
  579. {
  580. $columnID = PHPExcel_Cell :: stringFromColumnIndex($cell_ss['Index'] - 1);
  581. }
  582. $cellRange = $columnID . $rowID;
  583. if (! is_null($this->getReadFilter()))
  584. {
  585. if (! $this->getReadFilter()->readCell($columnID, $rowID, $worksheetName))
  586. {
  587. continue;
  588. }
  589. }
  590. if ((isset($cell_ss['MergeAcross'])) || (isset($cell_ss['MergeDown'])))
  591. {
  592. $columnTo = $columnID;
  593. if (isset($cell_ss['MergeAcross']))
  594. {
  595. $columnTo = PHPExcel_Cell :: stringFromColumnIndex(PHPExcel_Cell :: columnIndexFromString($columnID) + $cell_ss['MergeAcross'] - 1);
  596. }
  597. $rowTo = $rowID;
  598. if (isset($cell_ss['MergeDown']))
  599. {
  600. $rowTo = $rowTo + $cell_ss['MergeDown'];
  601. }
  602. $cellRange .= ':' . $columnTo . $rowTo;
  603. $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
  604. }
  605. $cellIsSet = $hasCalculatedValue = false;
  606. $cellDataFormula = '';
  607. if (isset($cell_ss['Formula']))
  608. {
  609. $cellDataFormula = $cell_ss['Formula'];
  610. // added this as a check for array formulas
  611. if (isset($cell_ss['ArrayRange']))
  612. {
  613. $cellDataCSEFormula = $cell_ss['ArrayRange'];
  614. // echo "found an array formula at ".$columnID.$rowID."<br />";
  615. }
  616. $hasCalculatedValue = true;
  617. }
  618. if (isset($cell->Data))
  619. {
  620. $cellValue = $cellData = $cell->Data;
  621. $type = PHPExcel_Cell_DataType :: TYPE_NULL;
  622. $cellData_ss = $cellData->attributes($namespaces['ss']);
  623. if (isset($cellData_ss['Type']))
  624. {
  625. $cellDataType = $cellData_ss['Type'];
  626. switch ($cellDataType)
  627. {
  628. /*
  629. const TYPE_STRING = 's';
  630. const TYPE_FORMULA = 'f';
  631. const TYPE_NUMERIC = 'n';
  632. const TYPE_BOOL = 'b';
  633. const TYPE_NULL = 's';
  634. const TYPE_INLINE = 'inlineStr';
  635. const TYPE_ERROR = 'e';
  636. */
  637. case 'String' :
  638. $type = PHPExcel_Cell_DataType :: TYPE_STRING;
  639. break;
  640. case 'Number' :
  641. $type = PHPExcel_Cell_DataType :: TYPE_NUMERIC;
  642. $cellValue = (float) $cellValue;
  643. if (floor($cellValue) == $cellValue)
  644. {
  645. $cellValue = (integer) $cellValue;
  646. }
  647. break;
  648. case 'Boolean' :
  649. $type = PHPExcel_Cell_DataType :: TYPE_BOOL;
  650. $cellValue = ($cellValue != 0);
  651. break;
  652. case 'DateTime' :
  653. $type = PHPExcel_Cell_DataType :: TYPE_NUMERIC;
  654. $cellValue = PHPExcel_Shared_Date :: PHPToExcel(strtotime($cellValue));
  655. break;
  656. case 'Error' :
  657. $type = PHPExcel_Cell_DataType :: TYPE_ERROR;
  658. break;
  659. }
  660. }
  661. if ($hasCalculatedValue)
  662. {
  663. // echo 'FORMULA<br />';
  664. $type = PHPExcel_Cell_DataType :: TYPE_FORMULA;
  665. $columnNumber = PHPExcel_Cell :: columnIndexFromString($columnID);
  666. if (substr($cellDataFormula, 0, 3) == 'of:')
  667. {
  668. $cellDataFormula = substr($cellDataFormula, 3);
  669. // echo 'Before: ',$cellDataFormula,'<br />';
  670. $temp = explode('"', $cellDataFormula);
  671. $key = false;
  672. foreach ($temp as &$value)
  673. {
  674. // Only replace in alternate array entries (i.e. non-quoted blocks)
  675. if ($key = ! $key)
  676. {
  677. $value = str_replace(array('[.', '.', ']'), '', $value);
  678. }
  679. }
  680. }
  681. else
  682. {
  683. // Convert R1C1 style references to A1 style references (but only when not quoted)
  684. // echo 'Before: ',$cellDataFormula,'<br />';
  685. $temp = explode('"', $cellDataFormula);
  686. $key = false;
  687. foreach ($temp as &$value)
  688. {
  689. // Only replace in alternate array entries (i.e. non-quoted blocks)
  690. if ($key = ! $key)
  691. {
  692. preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
  693. // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
  694. // through the formula from left to right. Reversing means that we work right to left.through
  695. // the formula
  696. $cellReferences = array_reverse($cellReferences);
  697. // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
  698. // then modify the formula to use that new reference
  699. foreach ($cellReferences as $cellReference)
  700. {
  701. $rowReference = $cellReference[2][0];
  702. // Empty R reference is the current row
  703. if ($rowReference == '')
  704. $rowReference = $rowID;
  705. // Bracketed R references are relative to the current row
  706. if ($rowReference{0} == '[')
  707. $rowReference = $rowID + trim($rowReference, '[]');
  708. $columnReference = $cellReference[4][0];
  709. // Empty C reference is the current column
  710. if ($columnReference == '')
  711. $columnReference = $columnNumber;
  712. // Bracketed C references are relative to the current column
  713. if ($columnReference{0} == '[')
  714. $columnReference = $columnNumber + trim($columnReference, '[]');
  715. $A1CellReference = PHPExcel_Cell :: stringFromColumnIndex($columnReference - 1) . $rowReference;
  716. $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0]));
  717. }
  718. }
  719. }
  720. }
  721. unset($value);
  722. // Then rebuild the formula string
  723. $cellDataFormula = implode('"', $temp);
  724. // echo 'After: ',$cellDataFormula,'<br />';
  725. }
  726. // echo 'Cell '.$columnID.$rowID.' is a '.$type.' with a value of '.(($hasCalculatedValue) ? $cellDataFormula : $cellValue).'<br />';
  727. //
  728. $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue), $type);
  729. if ($hasCalculatedValue)
  730. {
  731. // echo 'Formula result is '.$cellValue.'<br />';
  732. $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setCalculatedValue($cellValue);
  733. }
  734. $cellIsSet = $rowHasData = true;
  735. }
  736. if (isset($cell->Comment))
  737. {
  738. // echo '<b>comment found</b><br />';
  739. $commentAttributes = $cell->Comment->attributes($namespaces['ss']);
  740. $author = 'unknown';
  741. if (isset($commentAttributes->Author))
  742. {
  743. $author = (string) $commentAttributes->Author;
  744. // echo 'Author: ',$author,'<br />';
  745. }
  746. $node = $cell->Comment->Data->asXML();
  747. // $annotation = str_replace('html:','',substr($node,49,-10));
  748. // echo $annotation,'<br />';
  749. $annotation = strip_tags($node);
  750. // echo 'Annotation: ',$annotation,'<br />';
  751. $objPHPExcel->getActiveSheet()->getComment($columnID . $rowID)->setAuthor($author)->setText($this->_parseRichText($annotation));
  752. }
  753. if (($cellIsSet) && (isset($cell_ss['StyleID'])))
  754. {
  755. $style = (string) $cell_ss['StyleID'];
  756. // echo 'Cell style for '.$columnID.$rowID.' is '.$style.'<br />';
  757. if ((isset($this->_styles[$style])) && (count($this->_styles[$style]) > 0))
  758. {
  759. // echo 'Cell '.$columnID.$rowID.'<br />';
  760. // print_r($this->_styles[$style]);
  761. // echo '<br />';
  762. if (! $objPHPExcel->getActiveSheet()->cellExists($columnID . $rowID))
  763. {
  764. $objPHPExcel->getActiveSheet()->getCell($columnID . $rowID)->setValue(NULL);
  765. }
  766. $objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->_styles[$style]);
  767. }
  768. }
  769. ++ $columnID;
  770. }
  771. if ($rowHasData)
  772. {
  773. if (isset($row_ss['StyleID']))
  774. {
  775. $rowStyle = $row_ss['StyleID'];
  776. }
  777. if (isset($row_ss['Height']))
  778. {
  779. $rowHeight = $row_ss['Height'];
  780. // echo '<b>Setting row height to '.$rowHeight.'</b><br />';
  781. $objPHPExcel->getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight);
  782. }
  783. }
  784. ++ $rowID;
  785. }
  786. }
  787. ++ $worksheetID;
  788. }
  789. // Return
  790. return $objPHPExcel;
  791. }
  792. private function _parseRichText($is = '')
  793. {
  794. $value = new PHPExcel_RichText();
  795. $value->createText($is);
  796. return $value;
  797. }
  798. }