PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/includes/tools/phpMyAdmin/libraries/PHPExcel/PHPExcel/Reader/OOCalc.php

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