PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/app/webroot/Classes/PHPExcel/Reader/OOCalc.php

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