PageRenderTime 87ms CodeModel.GetById 45ms RepoModel.GetById 10ms app.codeStats 0ms

/reports/promotion/output/plugins/PHPExcel/Reader/OOCalc.php

https://bitbucket.org/lecturer34/hrmis
PHP | 532 lines | 309 code | 45 blank | 178 comment | 48 complexity | 2e90fee34234159ebd7c0cd90b9695fd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.5, 2010-12-10
  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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Reader_OOCalc 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. * Sheet index to read
  58. *
  59. * @var int
  60. */
  61. private $_sheetIndex = 0;
  62. /**
  63. * Formats
  64. *
  65. * @var array
  66. */
  67. private $_styles = array();
  68. /**
  69. * PHPExcel_Reader_IReadFilter instance
  70. *
  71. * @var PHPExcel_Reader_IReadFilter
  72. */
  73. private $_readFilter = null;
  74. /**
  75. * Read data only?
  76. *
  77. * @return boolean
  78. */
  79. public function getReadDataOnly() {
  80. return $this->_readDataOnly;
  81. }
  82. /**
  83. * Set read data only
  84. *
  85. * @param boolean $pValue
  86. * @return PHPExcel_Reader_OOCalc
  87. */
  88. public function setReadDataOnly($pValue = false) {
  89. $this->_readDataOnly = $pValue;
  90. return $this;
  91. }
  92. /**
  93. * Get which sheets to load
  94. *
  95. * @return mixed
  96. */
  97. public function getLoadSheetsOnly()
  98. {
  99. return $this->_loadSheetsOnly;
  100. }
  101. /**
  102. * Set which sheets to load
  103. *
  104. * @param mixed $value
  105. * @return PHPExcel_Reader_OOCalc
  106. */
  107. public function setLoadSheetsOnly($value = null)
  108. {
  109. $this->_loadSheetsOnly = is_array($value) ?
  110. $value : array($value);
  111. return $this;
  112. }
  113. /**
  114. * Set all sheets to load
  115. *
  116. * @return PHPExcel_Reader_OOCalc
  117. */
  118. public function setLoadAllSheets()
  119. {
  120. $this->_loadSheetsOnly = null;
  121. return $this;
  122. }
  123. /**
  124. * Read filter
  125. *
  126. * @return PHPExcel_Reader_IReadFilter
  127. */
  128. public function getReadFilter() {
  129. return $this->_readFilter;
  130. }
  131. /**
  132. * Set read filter
  133. *
  134. * @param PHPExcel_Reader_IReadFilter $pValue
  135. * @return PHPExcel_Reader_OOCalc
  136. */
  137. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  138. $this->_readFilter = $pValue;
  139. return $this;
  140. }
  141. /**
  142. * Create a new PHPExcel_Reader_OOCalc
  143. */
  144. public function __construct() {
  145. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  146. }
  147. /**
  148. * Can the current PHPExcel_Reader_IReader read the file?
  149. *
  150. * @param string $pFileName
  151. * @return boolean
  152. */
  153. public function canRead($pFilename)
  154. {
  155. // Check if zip class exists
  156. if (!class_exists('ZipArchive')) {
  157. return false;
  158. }
  159. // Check if file exists
  160. if (!file_exists($pFilename)) {
  161. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  162. }
  163. // Load file
  164. $zip = new ZipArchive;
  165. if ($zip->open($pFilename) === true) {
  166. // check if it is an OOXML archive
  167. $mimeType = $zip->getFromName("mimetype");
  168. $zip->close();
  169. return ($mimeType === 'application/vnd.oasis.opendocument.spreadsheet');
  170. }
  171. return false;
  172. }
  173. /**
  174. * Loads PHPExcel from file
  175. *
  176. * @param string $pFilename
  177. * @return PHPExcel
  178. * @throws Exception
  179. */
  180. public function load($pFilename)
  181. {
  182. // Create new PHPExcel
  183. $objPHPExcel = new PHPExcel();
  184. // Load into this instance
  185. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  186. }
  187. private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
  188. $styleAttributeValue = strtolower($styleAttributeValue);
  189. foreach($styleList as $style) {
  190. if ($styleAttributeValue == strtolower($style)) {
  191. $styleAttributeValue = $style;
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. /**
  198. * Loads PHPExcel from file into PHPExcel instance
  199. *
  200. * @param string $pFilename
  201. * @param PHPExcel $objPHPExcel
  202. * @return PHPExcel
  203. * @throws Exception
  204. */
  205. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  206. {
  207. // Check if file exists
  208. if (!file_exists($pFilename)) {
  209. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  210. }
  211. $timezoneObj = new DateTimeZone('Europe/London');
  212. $GMT = new DateTimeZone('UTC');
  213. $zip = new ZipArchive;
  214. if ($zip->open($pFilename) === true) {
  215. // echo '<h1>Meta Information</h1>';
  216. $xml = simplexml_load_string($zip->getFromName("meta.xml"));
  217. $namespacesMeta = $xml->getNamespaces(true);
  218. // echo '<pre>';
  219. // print_r($namespacesMeta);
  220. // echo '</pre><hr />';
  221. $docProps = $objPHPExcel->getProperties();
  222. $officeProperty = $xml->children($namespacesMeta['office']);
  223. foreach($officeProperty as $officePropertyData) {
  224. $officePropertyDC = array();
  225. if (isset($namespacesMeta['dc'])) {
  226. $officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
  227. }
  228. foreach($officePropertyDC as $propertyName => $propertyValue) {
  229. switch ($propertyName) {
  230. case 'title' :
  231. $docProps->setTitle($propertyValue);
  232. break;
  233. case 'subject' :
  234. $docProps->setSubject($propertyValue);
  235. break;
  236. case 'creator' :
  237. $docProps->setCreator($propertyValue);
  238. $docProps->setLastModifiedBy($propertyValue);
  239. break;
  240. case 'date' :
  241. $creationDate = strtotime($propertyValue);
  242. $docProps->setCreated($creationDate);
  243. $docProps->setModified($creationDate);
  244. break;
  245. case 'description' :
  246. $docProps->setDescription($propertyValue);
  247. break;
  248. }
  249. }
  250. $officePropertyMeta = array();
  251. if (isset($namespacesMeta['dc'])) {
  252. $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
  253. }
  254. foreach($officePropertyMeta as $propertyName => $propertyValue) {
  255. $propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
  256. switch ($propertyName) {
  257. case 'initial-creator' :
  258. $docProps->setCreator($propertyValue);
  259. break;
  260. case 'keyword' :
  261. $docProps->setKeywords($propertyValue);
  262. break;
  263. case 'creation-date' :
  264. $creationDate = strtotime($propertyValue);
  265. $docProps->setCreated($creationDate);
  266. break;
  267. case 'user-defined' :
  268. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
  269. foreach ($propertyValueAttributes as $key => $value) {
  270. if ($key == 'name') {
  271. $propertyValueName = (string) $value;
  272. } elseif($key == 'value-type') {
  273. switch ($value) {
  274. case 'date' :
  275. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'date');
  276. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_DATE;
  277. break;
  278. case 'boolean' :
  279. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'bool');
  280. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_BOOLEAN;
  281. break;
  282. case 'float' :
  283. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue,'r4');
  284. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_FLOAT;
  285. break;
  286. default :
  287. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
  288. }
  289. }
  290. }
  291. $docProps->setCustomProperty($propertyValueName,$propertyValue,$propertyValueType);
  292. break;
  293. }
  294. }
  295. }
  296. // echo '<h1>Workbook Content</h1>';
  297. $xml = simplexml_load_string($zip->getFromName("content.xml"));
  298. $namespacesContent = $xml->getNamespaces(true);
  299. // echo '<pre>';
  300. // print_r($namespacesContent);
  301. // echo '</pre><hr />';
  302. $workbook = $xml->children($namespacesContent['office']);
  303. foreach($workbook->body->spreadsheet as $workbookData) {
  304. $workbookData = $workbookData->children($namespacesContent['table']);
  305. $worksheetID = 0;
  306. foreach($workbookData->table as $worksheetDataSet) {
  307. $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
  308. // print_r($worksheetData);
  309. // echo '<br />';
  310. $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
  311. // print_r($worksheetDataAttributes);
  312. // echo '<br />';
  313. if ((isset($this->_loadSheetsOnly)) && (isset($worksheetDataAttributes['name'])) &&
  314. (!in_array($worksheetDataAttributes['name'], $this->_loadSheetsOnly))) {
  315. continue;
  316. }
  317. // echo '<h2>Worksheet '.$worksheetDataAttributes['name'].'</h2>';
  318. // Create new Worksheet
  319. $objPHPExcel->createSheet();
  320. $objPHPExcel->setActiveSheetIndex($worksheetID);
  321. if (isset($worksheetDataAttributes['name'])) {
  322. $worksheetName = (string) $worksheetDataAttributes['name'];
  323. $objPHPExcel->getActiveSheet()->setTitle($worksheetName);
  324. }
  325. $rowID = 1;
  326. foreach($worksheetData as $key => $rowData) {
  327. // echo '<b>'.$key.'</b><br />';
  328. switch ($key) {
  329. case 'table-header-rows':
  330. foreach ($rowData as $key=>$cellData) {
  331. $rowData = $cellData;
  332. break;
  333. }
  334. case 'table-row' :
  335. $columnID = 'A';
  336. foreach($rowData as $key => $cellData) {
  337. if (!is_null($this->getReadFilter())) {
  338. if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
  339. continue;
  340. }
  341. }
  342. // echo '<b>'.$columnID.$rowID.'</b><br />';
  343. $cellDataText = $cellData->children($namespacesContent['text']);
  344. $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
  345. $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
  346. // echo 'Office Attributes: ';
  347. // print_r($cellDataOfficeAttributes);
  348. // echo '<br />Table Attributes: ';
  349. // print_r($cellDataTableAttributes);
  350. // echo '<br />Cell Data Text';
  351. // print_r($cellDataText);
  352. // echo '<br />';
  353. //
  354. $type = $formatting = $hyperlink = null;
  355. $hasCalculatedValue = false;
  356. $cellDataFormula = '';
  357. if (isset($cellDataTableAttributes['formula'])) {
  358. $cellDataFormula = $cellDataTableAttributes['formula'];
  359. $hasCalculatedValue = true;
  360. }
  361. if (isset($cellDataText->p)) {
  362. // echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
  363. switch ($cellDataOfficeAttributes['value-type']) {
  364. case 'string' :
  365. $type = PHPExcel_Cell_DataType::TYPE_STRING;
  366. $dataValue = $cellDataText->p;
  367. if (isset($dataValue->a)) {
  368. $dataValue = $dataValue->a;
  369. $cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']);
  370. $hyperlink = $cellXLinkAttributes['href'];
  371. }
  372. break;
  373. case 'boolean' :
  374. $type = PHPExcel_Cell_DataType::TYPE_BOOL;
  375. $dataValue = ($cellDataText->p == 'TRUE') ? True : False;
  376. break;
  377. case 'float' :
  378. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  379. $dataValue = (float) $cellDataOfficeAttributes['value'];
  380. if (floor($dataValue) == $dataValue) {
  381. $dataValue = (integer) $dataValue;
  382. }
  383. break;
  384. case 'date' :
  385. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  386. $dateObj = new DateTime($cellDataOfficeAttributes['date-value'], $GMT);
  387. $dateObj->setTimeZone($timezoneObj);
  388. list($year,$month,$day,$hour,$minute,$second) = explode(' ',$dateObj->format('Y m d H i s'));
  389. $dataValue = PHPExcel_Shared_Date::FormattedPHPToExcel($year,$month,$day,$hour,$minute,$second);
  390. if ($dataValue != floor($dataValue)) {
  391. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15.' '.PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  392. } else {
  393. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15;
  394. }
  395. break;
  396. case 'time' :
  397. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  398. $dataValue = PHPExcel_Shared_Date::PHPToExcel(strtotime('01-01-1970 '.implode(':',sscanf($cellDataOfficeAttributes['time-value'],'PT%dH%dM%dS'))));
  399. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  400. break;
  401. }
  402. // echo 'Data value is '.$dataValue.'<br />';
  403. // if (!is_null($hyperlink)) {
  404. // echo 'Hyperlink is '.$hyperlink.'<br />';
  405. // }
  406. }
  407. if ($hasCalculatedValue) {
  408. $type = PHPExcel_Cell_DataType::TYPE_FORMULA;
  409. // echo 'Formula: '.$cellDataFormula.'<br />';
  410. $cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,':=')+1);
  411. $temp = explode('"',$cellDataFormula);
  412. foreach($temp as $key => &$value) {
  413. // Only replace in alternate array entries (i.e. non-quoted blocks)
  414. if (($key % 2) == 0) {
  415. $value = preg_replace('/\[\.(.*):\.(.*)\]/Ui','$1:$2',$value);
  416. $value = preg_replace('/\[\.(.*)\]/Ui','$1',$value);
  417. $value = PHPExcel_Calculation::_translateSeparator(';',',',$value,$inBraces);
  418. }
  419. }
  420. unset($value);
  421. // Then rebuild the formula string
  422. $cellDataFormula = implode('"',$temp);
  423. // echo 'Adjusted Formula: '.$cellDataFormula.'<br />';
  424. }
  425. if (!is_null($type)) {
  426. $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $dataValue),$type);
  427. if ($hasCalculatedValue) {
  428. // echo 'Forumla result is '.$dataValue.'<br />';
  429. $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setCalculatedValue($dataValue);
  430. }
  431. if (($cellDataOfficeAttributes['value-type'] == 'date') ||
  432. ($cellDataOfficeAttributes['value-type'] == 'time')) {
  433. $objPHPExcel->getActiveSheet()->getStyle($columnID.$rowID)->getNumberFormat()->setFormatCode($formatting);
  434. }
  435. if (!is_null($hyperlink)) {
  436. $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->getHyperlink()->setUrl($hyperlink);
  437. }
  438. }
  439. // Merged cells
  440. if ((isset($cellDataTableAttributes['number-columns-spanned'])) || (isset($cellDataTableAttributes['number-rows-spanned']))) {
  441. $columnTo = $columnID;
  442. if (isset($cellDataTableAttributes['number-columns-spanned'])) {
  443. $columnTo = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-spanned'] -2);
  444. }
  445. $rowTo = $rowID;
  446. if (isset($cellDataTableAttributes['number-rows-spanned'])) {
  447. $rowTo = $rowTo + $cellDataTableAttributes['number-rows-spanned'] - 1;
  448. }
  449. $cellRange = $columnID.$rowID.':'.$columnTo.$rowTo;
  450. $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
  451. }
  452. if (isset($cellDataTableAttributes['number-columns-repeated'])) {
  453. // echo 'Repeated '.$cellDataTableAttributes['number-columns-repeated'].' times<br />';
  454. $columnID = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-repeated'] - 2);
  455. }
  456. ++$columnID;
  457. }
  458. ++$rowID;
  459. break;
  460. }
  461. }
  462. ++$worksheetID;
  463. }
  464. }
  465. }
  466. // Return
  467. return $objPHPExcel;
  468. }
  469. /**
  470. * Get sheet index
  471. *
  472. * @return int
  473. */
  474. public function getSheetIndex() {
  475. return $this->_sheetIndex;
  476. }
  477. /**
  478. * Set sheet index
  479. *
  480. * @param int $pValue Sheet index
  481. * @return PHPExcel_Reader_OOCalc
  482. */
  483. public function setSheetIndex($pValue = 0) {
  484. $this->_sheetIndex = $pValue;
  485. return $this;
  486. }
  487. }