PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/PHPExcel/PHPExcel/Reader/Excel2003XML.php

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