PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/PHPExcel/Reader/Gnumeric.php

https://github.com/iGroup/PHPExcel
PHP | 866 lines | 663 code | 76 blank | 127 comment | 80 complexity | b5e82094dfb62772abb044355753a9cd MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  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_Gnumeric
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Reader
  40. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Reader_Gnumeric extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  43. {
  44. /**
  45. * Formats
  46. *
  47. * @var array
  48. */
  49. private $_styles = array();
  50. /**
  51. * Shared Expressions
  52. *
  53. * @var array
  54. */
  55. private $_expressions = array();
  56. private $_referenceHelper = null;
  57. /**
  58. * Create a new PHPExcel_Reader_Gnumeric
  59. */
  60. public function __construct() {
  61. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  62. $this->_referenceHelper = PHPExcel_ReferenceHelper::getInstance();
  63. }
  64. /**
  65. * Can the current PHPExcel_Reader_IReader read the file?
  66. *
  67. * @param string $pFileName
  68. * @return boolean
  69. * @throws PHPExcel_Reader_Exception
  70. */
  71. public function canRead($pFilename)
  72. {
  73. // Check if file exists
  74. if (!file_exists($pFilename)) {
  75. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  76. }
  77. // Check if gzlib functions are available
  78. if (!function_exists('gzread')) {
  79. throw new PHPExcel_Reader_Exception("gzlib library is not enabled");
  80. }
  81. // Read signature data (first 3 bytes)
  82. $fh = fopen($pFilename, 'r');
  83. $data = fread($fh, 2);
  84. fclose($fh);
  85. if ($data != chr(0x1F).chr(0x8B)) {
  86. return false;
  87. }
  88. return true;
  89. }
  90. /**
  91. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  92. *
  93. * @param string $pFilename
  94. * @throws PHPExcel_Reader_Exception
  95. */
  96. public function listWorksheetInfo($pFilename)
  97. {
  98. // Check if file exists
  99. if (!file_exists($pFilename)) {
  100. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  101. }
  102. $gFileData = $this->_gzfileGetContents($pFilename);
  103. $xml = simplexml_load_string($gFileData);
  104. $namespacesMeta = $xml->getNamespaces(true);
  105. $gnmXML = $xml->children($namespacesMeta['gnm']);
  106. $worksheetInfo = array();
  107. foreach ($gnmXML->Sheets->Sheet as $sheet) {
  108. $tmpInfo = array();
  109. $tmpInfo['worksheetName'] = (string) $sheet->Name;
  110. $tmpInfo['lastColumnLetter'] = 'A';
  111. $tmpInfo['lastColumnIndex'] = 0;
  112. $tmpInfo['totalRows'] = 0;
  113. $tmpInfo['totalColumns'] = 0;
  114. foreach ($sheet->Cells->Cell as $cell) {
  115. $cellAttributes = $cell->attributes();
  116. $rowIndex = (int) $cellAttributes->Row + 1;
  117. $columnIndex = (int) $cellAttributes->Col;
  118. $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
  119. $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
  120. }
  121. $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
  122. $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
  123. $worksheetInfo[] = $tmpInfo;
  124. }
  125. return $worksheetInfo;
  126. }
  127. private function _gzfileGetContents($filename) {
  128. $file = @gzopen($filename, 'rb');
  129. if ($file !== false) {
  130. $data = '';
  131. while (!gzeof($file)) {
  132. $data .= gzread($file, 1024);
  133. }
  134. gzclose($file);
  135. }
  136. return $data;
  137. }
  138. /**
  139. * Loads PHPExcel from file
  140. *
  141. * @param string $pFilename
  142. * @return PHPExcel
  143. * @throws PHPExcel_Reader_Exception
  144. */
  145. public function load($pFilename)
  146. {
  147. // Create new PHPExcel
  148. $objPHPExcel = new PHPExcel();
  149. // Load into this instance
  150. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  151. }
  152. /**
  153. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  154. *
  155. * @param string $pFilename
  156. * @throws PHPExcel_Reader_Exception
  157. */
  158. public function listWorksheetNames($pFilename)
  159. {
  160. // Check if file exists
  161. if (!file_exists($pFilename)) {
  162. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  163. }
  164. $gFileData = $this->_gzfileGetContents($pFilename);
  165. $xml = simplexml_load_string($gFileData);
  166. $namespacesMeta = $xml->getNamespaces(true);
  167. $gnmXML = $xml->children($namespacesMeta['gnm']);
  168. $worksheetNames = array();
  169. foreach($gnmXML->Sheets->Sheet as $sheet) {
  170. $worksheetNames[] = (string) $sheet->Name;
  171. }
  172. return $worksheetNames;
  173. }
  174. /**
  175. * Loads PHPExcel from file into PHPExcel instance
  176. *
  177. * @param string $pFilename
  178. * @param PHPExcel $objPHPExcel
  179. * @return PHPExcel
  180. * @throws PHPExcel_Reader_Exception
  181. */
  182. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  183. {
  184. // Check if file exists
  185. if (!file_exists($pFilename)) {
  186. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  187. }
  188. $timezoneObj = new DateTimeZone('Europe/London');
  189. $GMT = new DateTimeZone('UTC');
  190. $gFileData = $this->_gzfileGetContents($pFilename);
  191. // echo '<pre>';
  192. // echo htmlentities($gFileData,ENT_QUOTES,'UTF-8');
  193. // echo '</pre><hr />';
  194. //
  195. $xml = simplexml_load_string($gFileData);
  196. $namespacesMeta = $xml->getNamespaces(true);
  197. // var_dump($namespacesMeta);
  198. //
  199. $gnmXML = $xml->children($namespacesMeta['gnm']);
  200. $docProps = $objPHPExcel->getProperties();
  201. // Document Properties are held differently, depending on the version of Gnumeric
  202. if (isset($namespacesMeta['office'])) {
  203. $officeXML = $xml->children($namespacesMeta['office']);
  204. $officeDocXML = $officeXML->{'document-meta'};
  205. $officeDocMetaXML = $officeDocXML->meta;
  206. foreach($officeDocMetaXML as $officePropertyData) {
  207. $officePropertyDC = array();
  208. if (isset($namespacesMeta['dc'])) {
  209. $officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
  210. }
  211. foreach($officePropertyDC as $propertyName => $propertyValue) {
  212. $propertyValue = (string) $propertyValue;
  213. switch ($propertyName) {
  214. case 'title' :
  215. $docProps->setTitle(trim($propertyValue));
  216. break;
  217. case 'subject' :
  218. $docProps->setSubject(trim($propertyValue));
  219. break;
  220. case 'creator' :
  221. $docProps->setCreator(trim($propertyValue));
  222. $docProps->setLastModifiedBy(trim($propertyValue));
  223. break;
  224. case 'date' :
  225. $creationDate = strtotime(trim($propertyValue));
  226. $docProps->setCreated($creationDate);
  227. $docProps->setModified($creationDate);
  228. break;
  229. case 'description' :
  230. $docProps->setDescription(trim($propertyValue));
  231. break;
  232. }
  233. }
  234. $officePropertyMeta = array();
  235. if (isset($namespacesMeta['meta'])) {
  236. $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
  237. }
  238. foreach($officePropertyMeta as $propertyName => $propertyValue) {
  239. $attributes = $propertyValue->attributes($namespacesMeta['meta']);
  240. $propertyValue = (string) $propertyValue;
  241. switch ($propertyName) {
  242. case 'keyword' :
  243. $docProps->setKeywords(trim($propertyValue));
  244. break;
  245. case 'initial-creator' :
  246. $docProps->setCreator(trim($propertyValue));
  247. $docProps->setLastModifiedBy(trim($propertyValue));
  248. break;
  249. case 'creation-date' :
  250. $creationDate = strtotime(trim($propertyValue));
  251. $docProps->setCreated($creationDate);
  252. $docProps->setModified($creationDate);
  253. break;
  254. case 'user-defined' :
  255. list(,$attrName) = explode(':',$attributes['name']);
  256. switch ($attrName) {
  257. case 'publisher' :
  258. $docProps->setCompany(trim($propertyValue));
  259. break;
  260. case 'category' :
  261. $docProps->setCategory(trim($propertyValue));
  262. break;
  263. case 'manager' :
  264. $docProps->setManager(trim($propertyValue));
  265. break;
  266. }
  267. break;
  268. }
  269. }
  270. }
  271. } elseif (isset($gnmXML->Summary)) {
  272. foreach($gnmXML->Summary->Item as $summaryItem) {
  273. $propertyName = $summaryItem->name;
  274. $propertyValue = $summaryItem->{'val-string'};
  275. switch ($propertyName) {
  276. case 'title' :
  277. $docProps->setTitle(trim($propertyValue));
  278. break;
  279. case 'comments' :
  280. $docProps->setDescription(trim($propertyValue));
  281. break;
  282. case 'keywords' :
  283. $docProps->setKeywords(trim($propertyValue));
  284. break;
  285. case 'category' :
  286. $docProps->setCategory(trim($propertyValue));
  287. break;
  288. case 'manager' :
  289. $docProps->setManager(trim($propertyValue));
  290. break;
  291. case 'author' :
  292. $docProps->setCreator(trim($propertyValue));
  293. $docProps->setLastModifiedBy(trim($propertyValue));
  294. break;
  295. case 'company' :
  296. $docProps->setCompany(trim($propertyValue));
  297. break;
  298. }
  299. }
  300. }
  301. $worksheetID = 0;
  302. foreach($gnmXML->Sheets->Sheet as $sheet) {
  303. $worksheetName = (string) $sheet->Name;
  304. // echo '<b>Worksheet: ',$worksheetName,'</b><br />';
  305. if ((isset($this->_loadSheetsOnly)) && (!in_array($worksheetName, $this->_loadSheetsOnly))) {
  306. continue;
  307. }
  308. $maxRow = $maxCol = 0;
  309. // Create new Worksheet
  310. $objPHPExcel->createSheet();
  311. $objPHPExcel->setActiveSheetIndex($worksheetID);
  312. // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in formula
  313. // cells... during the load, all formulae should be correct, and we're simply bringing the worksheet
  314. // name in line with the formula, not the reverse
  315. $objPHPExcel->getActiveSheet()->setTitle($worksheetName,false);
  316. if ((!$this->_readDataOnly) && (isset($sheet->PrintInformation))) {
  317. if (isset($sheet->PrintInformation->Margins)) {
  318. foreach($sheet->PrintInformation->Margins->children('gnm',TRUE) as $key => $margin) {
  319. $marginAttributes = $margin->attributes();
  320. $marginSize = 72 / 100; // Default
  321. switch($marginAttributes['PrefUnit']) {
  322. case 'mm' :
  323. $marginSize = intval($marginAttributes['Points']) / 100;
  324. break;
  325. }
  326. switch($key) {
  327. case 'top' :
  328. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop($marginSize);
  329. break;
  330. case 'bottom' :
  331. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom($marginSize);
  332. break;
  333. case 'left' :
  334. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft($marginSize);
  335. break;
  336. case 'right' :
  337. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight($marginSize);
  338. break;
  339. case 'header' :
  340. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader($marginSize);
  341. break;
  342. case 'footer' :
  343. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter($marginSize);
  344. break;
  345. }
  346. }
  347. }
  348. }
  349. foreach($sheet->Cells->Cell as $cell) {
  350. $cellAttributes = $cell->attributes();
  351. $row = (int) $cellAttributes->Row + 1;
  352. $column = (int) $cellAttributes->Col;
  353. if ($row > $maxRow) $maxRow = $row;
  354. if ($column > $maxCol) $maxCol = $column;
  355. $column = PHPExcel_Cell::stringFromColumnIndex($column);
  356. // Read cell?
  357. if ($this->getReadFilter() !== NULL) {
  358. if (!$this->getReadFilter()->readCell($column, $row, $worksheetName)) {
  359. continue;
  360. }
  361. }
  362. $ValueType = $cellAttributes->ValueType;
  363. $ExprID = (string) $cellAttributes->ExprID;
  364. // echo 'Cell ',$column,$row,'<br />';
  365. // echo 'Type is ',$ValueType,'<br />';
  366. // echo 'Value is ',$cell,'<br />';
  367. $type = PHPExcel_Cell_DataType::TYPE_FORMULA;
  368. if ($ExprID > '') {
  369. if (((string) $cell) > '') {
  370. $this->_expressions[$ExprID] = array( 'column' => $cellAttributes->Col,
  371. 'row' => $cellAttributes->Row,
  372. 'formula' => (string) $cell
  373. );
  374. // echo 'NEW EXPRESSION ',$ExprID,'<br />';
  375. } else {
  376. $expression = $this->_expressions[$ExprID];
  377. $cell = $this->_referenceHelper->updateFormulaReferences( $expression['formula'],
  378. 'A1',
  379. $cellAttributes->Col - $expression['column'],
  380. $cellAttributes->Row - $expression['row'],
  381. $worksheetName
  382. );
  383. // echo 'SHARED EXPRESSION ',$ExprID,'<br />';
  384. // echo 'New Value is ',$cell,'<br />';
  385. }
  386. $type = PHPExcel_Cell_DataType::TYPE_FORMULA;
  387. } else {
  388. switch($ValueType) {
  389. case '10' : // NULL
  390. $type = PHPExcel_Cell_DataType::TYPE_NULL;
  391. break;
  392. case '20' : // Boolean
  393. $type = PHPExcel_Cell_DataType::TYPE_BOOL;
  394. $cell = ($cell == 'TRUE') ? True : False;
  395. break;
  396. case '30' : // Integer
  397. $cell = intval($cell);
  398. case '40' : // Float
  399. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  400. break;
  401. case '50' : // Error
  402. $type = PHPExcel_Cell_DataType::TYPE_ERROR;
  403. break;
  404. case '60' : // String
  405. $type = PHPExcel_Cell_DataType::TYPE_STRING;
  406. break;
  407. case '70' : // Cell Range
  408. case '80' : // Array
  409. }
  410. }
  411. $objPHPExcel->getActiveSheet()->getCell($column.$row)->setValueExplicit($cell,$type);
  412. }
  413. if ((!$this->_readDataOnly) && (isset($sheet->Objects))) {
  414. foreach($sheet->Objects->children('gnm',TRUE) as $key => $comment) {
  415. $commentAttributes = $comment->attributes();
  416. // Only comment objects are handled at the moment
  417. if ($commentAttributes->Text) {
  418. $objPHPExcel->getActiveSheet()->getComment( (string)$commentAttributes->ObjectBound )
  419. ->setAuthor( (string)$commentAttributes->Author )
  420. ->setText($this->_parseRichText((string)$commentAttributes->Text) );
  421. }
  422. }
  423. }
  424. // echo '$maxCol=',$maxCol,'; $maxRow=',$maxRow,'<br />';
  425. //
  426. foreach($sheet->Styles->StyleRegion as $styleRegion) {
  427. $styleAttributes = $styleRegion->attributes();
  428. if (($styleAttributes['startRow'] <= $maxRow) &&
  429. ($styleAttributes['startCol'] <= $maxCol)) {
  430. $startColumn = PHPExcel_Cell::stringFromColumnIndex((int) $styleAttributes['startCol']);
  431. $startRow = $styleAttributes['startRow'] + 1;
  432. $endColumn = ($styleAttributes['endCol'] > $maxCol) ? $maxCol : (int) $styleAttributes['endCol'];
  433. $endColumn = PHPExcel_Cell::stringFromColumnIndex($endColumn);
  434. $endRow = ($styleAttributes['endRow'] > $maxRow) ? $maxRow : $styleAttributes['endRow'];
  435. $endRow += 1;
  436. $cellRange = $startColumn.$startRow.':'.$endColumn.$endRow;
  437. // echo $cellRange,'<br />';
  438. $styleAttributes = $styleRegion->Style->attributes();
  439. // var_dump($styleAttributes);
  440. // echo '<br />';
  441. // We still set the number format mask for date/time values, even if _readDataOnly is true
  442. if ((!$this->_readDataOnly) ||
  443. (PHPExcel_Shared_Date::isDateTimeFormatCode($styleArray['numberformat']['code']))) {
  444. $styleArray = array();
  445. $styleArray['numberformat']['code'] = (string) $styleAttributes['Format'];
  446. // If _readDataOnly is false, we set all formatting information
  447. if (!$this->_readDataOnly) {
  448. switch($styleAttributes['HAlign']) {
  449. case '1' :
  450. $styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_GENERAL;
  451. break;
  452. case '2' :
  453. $styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_LEFT;
  454. break;
  455. case '4' :
  456. $styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_RIGHT;
  457. break;
  458. case '8' :
  459. $styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_CENTER;
  460. break;
  461. case '16' :
  462. case '64' :
  463. $styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS;
  464. break;
  465. case '32' :
  466. $styleArray['alignment']['horizontal'] = PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY;
  467. break;
  468. }
  469. switch($styleAttributes['VAlign']) {
  470. case '1' :
  471. $styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_TOP;
  472. break;
  473. case '2' :
  474. $styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_BOTTOM;
  475. break;
  476. case '4' :
  477. $styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_CENTER;
  478. break;
  479. case '8' :
  480. $styleArray['alignment']['vertical'] = PHPExcel_Style_Alignment::VERTICAL_JUSTIFY;
  481. break;
  482. }
  483. $styleArray['alignment']['wrap'] = ($styleAttributes['WrapText'] == '1') ? True : False;
  484. $styleArray['alignment']['shrinkToFit'] = ($styleAttributes['ShrinkToFit'] == '1') ? True : False;
  485. $styleArray['alignment']['indent'] = (intval($styleAttributes["Indent"]) > 0) ? $styleAttributes["indent"] : 0;
  486. $RGB = self::_parseGnumericColour($styleAttributes["Fore"]);
  487. $styleArray['font']['color']['rgb'] = $RGB;
  488. $RGB = self::_parseGnumericColour($styleAttributes["Back"]);
  489. $shade = $styleAttributes["Shade"];
  490. if (($RGB != '000000') || ($shade != '0')) {
  491. $styleArray['fill']['color']['rgb'] = $styleArray['fill']['startcolor']['rgb'] = $RGB;
  492. $RGB2 = self::_parseGnumericColour($styleAttributes["PatternColor"]);
  493. $styleArray['fill']['endcolor']['rgb'] = $RGB2;
  494. switch($shade) {
  495. case '1' :
  496. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_SOLID;
  497. break;
  498. case '2' :
  499. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR;
  500. break;
  501. case '3' :
  502. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_GRADIENT_PATH;
  503. break;
  504. case '4' :
  505. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKDOWN;
  506. break;
  507. case '5' :
  508. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKGRAY;
  509. break;
  510. case '6' :
  511. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKGRID;
  512. break;
  513. case '7' :
  514. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKHORIZONTAL;
  515. break;
  516. case '8' :
  517. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKTRELLIS;
  518. break;
  519. case '9' :
  520. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKUP;
  521. break;
  522. case '10' :
  523. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_DARKVERTICAL;
  524. break;
  525. case '11' :
  526. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_GRAY0625;
  527. break;
  528. case '12' :
  529. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_GRAY125;
  530. break;
  531. case '13' :
  532. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTDOWN;
  533. break;
  534. case '14' :
  535. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRAY;
  536. break;
  537. case '15' :
  538. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTGRID;
  539. break;
  540. case '16' :
  541. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTHORIZONTAL;
  542. break;
  543. case '17' :
  544. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTTRELLIS;
  545. break;
  546. case '18' :
  547. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTUP;
  548. break;
  549. case '19' :
  550. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_LIGHTVERTICAL;
  551. break;
  552. case '20' :
  553. $styleArray['fill']['type'] = PHPExcel_Style_Fill::FILL_PATTERN_MEDIUMGRAY;
  554. break;
  555. }
  556. }
  557. $fontAttributes = $styleRegion->Style->Font->attributes();
  558. // var_dump($fontAttributes);
  559. // echo '<br />';
  560. $styleArray['font']['name'] = (string) $styleRegion->Style->Font;
  561. $styleArray['font']['size'] = intval($fontAttributes['Unit']);
  562. $styleArray['font']['bold'] = ($fontAttributes['Bold'] == '1') ? True : False;
  563. $styleArray['font']['italic'] = ($fontAttributes['Italic'] == '1') ? True : False;
  564. $styleArray['font']['strike'] = ($fontAttributes['StrikeThrough'] == '1') ? True : False;
  565. switch($fontAttributes['Underline']) {
  566. case '1' :
  567. $styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_SINGLE;
  568. break;
  569. case '2' :
  570. $styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_DOUBLE;
  571. break;
  572. case '3' :
  573. $styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING;
  574. break;
  575. case '4' :
  576. $styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING;
  577. break;
  578. default :
  579. $styleArray['font']['underline'] = PHPExcel_Style_Font::UNDERLINE_NONE;
  580. break;
  581. }
  582. switch($fontAttributes['Script']) {
  583. case '1' :
  584. $styleArray['font']['superScript'] = True;
  585. break;
  586. case '-1' :
  587. $styleArray['font']['subScript'] = True;
  588. break;
  589. }
  590. if (isset($styleRegion->Style->StyleBorder)) {
  591. if (isset($styleRegion->Style->StyleBorder->Top)) {
  592. $styleArray['borders']['top'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Top->attributes());
  593. }
  594. if (isset($styleRegion->Style->StyleBorder->Bottom)) {
  595. $styleArray['borders']['bottom'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Bottom->attributes());
  596. }
  597. if (isset($styleRegion->Style->StyleBorder->Left)) {
  598. $styleArray['borders']['left'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Left->attributes());
  599. }
  600. if (isset($styleRegion->Style->StyleBorder->Right)) {
  601. $styleArray['borders']['right'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Right->attributes());
  602. }
  603. if ((isset($styleRegion->Style->StyleBorder->Diagonal)) && (isset($styleRegion->Style->StyleBorder->{'Rev-Diagonal'}))) {
  604. $styleArray['borders']['diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
  605. $styleArray['borders']['diagonaldirection'] = PHPExcel_Style_Borders::DIAGONAL_BOTH;
  606. } elseif (isset($styleRegion->Style->StyleBorder->Diagonal)) {
  607. $styleArray['borders']['diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->Diagonal->attributes());
  608. $styleArray['borders']['diagonaldirection'] = PHPExcel_Style_Borders::DIAGONAL_UP;
  609. } elseif (isset($styleRegion->Style->StyleBorder->{'Rev-Diagonal'})) {
  610. $styleArray['borders']['diagonal'] = self::_parseBorderAttributes($styleRegion->Style->StyleBorder->{'Rev-Diagonal'}->attributes());
  611. $styleArray['borders']['diagonaldirection'] = PHPExcel_Style_Borders::DIAGONAL_DOWN;
  612. }
  613. }
  614. if (isset($styleRegion->Style->HyperLink)) {
  615. // TO DO
  616. $hyperlink = $styleRegion->Style->HyperLink->attributes();
  617. }
  618. }
  619. // var_dump($styleArray);
  620. // echo '<br />';
  621. $objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($styleArray);
  622. }
  623. }
  624. }
  625. if ((!$this->_readDataOnly) && (isset($sheet->Cols))) {
  626. // Column Widths
  627. $columnAttributes = $sheet->Cols->attributes();
  628. $defaultWidth = $columnAttributes['DefaultSizePts'] / 5.4;
  629. $c = 0;
  630. foreach($sheet->Cols->ColInfo as $columnOverride) {
  631. $columnAttributes = $columnOverride->attributes();
  632. $column = $columnAttributes['No'];
  633. $columnWidth = $columnAttributes['Unit'] / 5.4;
  634. $hidden = ((isset($columnAttributes['Hidden'])) && ($columnAttributes['Hidden'] == '1')) ? true : false;
  635. $columnCount = (isset($columnAttributes['Count'])) ? $columnAttributes['Count'] : 1;
  636. while ($c < $column) {
  637. $objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setWidth($defaultWidth);
  638. ++$c;
  639. }
  640. while (($c < ($column+$columnCount)) && ($c <= $maxCol)) {
  641. $objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setWidth($columnWidth);
  642. if ($hidden) {
  643. $objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setVisible(false);
  644. }
  645. ++$c;
  646. }
  647. }
  648. while ($c <= $maxCol) {
  649. $objPHPExcel->getActiveSheet()->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($c))->setWidth($defaultWidth);
  650. ++$c;
  651. }
  652. }
  653. if ((!$this->_readDataOnly) && (isset($sheet->Rows))) {
  654. // Row Heights
  655. $rowAttributes = $sheet->Rows->attributes();
  656. $defaultHeight = $rowAttributes['DefaultSizePts'];
  657. $r = 0;
  658. foreach($sheet->Rows->RowInfo as $rowOverride) {
  659. $rowAttributes = $rowOverride->attributes();
  660. $row = $rowAttributes['No'];
  661. $rowHeight = $rowAttributes['Unit'];
  662. $hidden = ((isset($rowAttributes['Hidden'])) && ($rowAttributes['Hidden'] == '1')) ? true : false;
  663. $rowCount = (isset($rowAttributes['Count'])) ? $rowAttributes['Count'] : 1;
  664. while ($r < $row) {
  665. ++$r;
  666. $objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
  667. }
  668. while (($r < ($row+$rowCount)) && ($r < $maxRow)) {
  669. ++$r;
  670. $objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($rowHeight);
  671. if ($hidden) {
  672. $objPHPExcel->getActiveSheet()->getRowDimension($r)->setVisible(false);
  673. }
  674. }
  675. }
  676. while ($r < $maxRow) {
  677. ++$r;
  678. $objPHPExcel->getActiveSheet()->getRowDimension($r)->setRowHeight($defaultHeight);
  679. }
  680. }
  681. // Handle Merged Cells in this worksheet
  682. if (isset($sheet->MergedRegions)) {
  683. foreach($sheet->MergedRegions->Merge as $mergeCells) {
  684. if (strpos($mergeCells,':') !== FALSE) {
  685. $objPHPExcel->getActiveSheet()->mergeCells($mergeCells);
  686. }
  687. }
  688. }
  689. $worksheetID++;
  690. }
  691. // Loop through definedNames (global named ranges)
  692. if (isset($gnmXML->Names)) {
  693. foreach($gnmXML->Names->Name as $namedRange) {
  694. $name = (string) $namedRange->name;
  695. $range = (string) $namedRange->value;
  696. if (stripos($range, '#REF!') !== false) {
  697. continue;
  698. }
  699. $range = explode('!',$range);
  700. $range[0] = trim($range[0],"'");;
  701. if ($worksheet = $objPHPExcel->getSheetByName($range[0])) {
  702. $extractedRange = str_replace('$', '', $range[1]);
  703. $objPHPExcel->addNamedRange( new PHPExcel_NamedRange($name, $worksheet, $extractedRange) );
  704. }
  705. }
  706. }
  707. // Return
  708. return $objPHPExcel;
  709. }
  710. private static function _parseBorderAttributes($borderAttributes)
  711. {
  712. $styleArray = array();
  713. if (isset($borderAttributes["Color"])) {
  714. $RGB = self::_parseGnumericColour($borderAttributes["Color"]);
  715. $styleArray['color']['rgb'] = $RGB;
  716. }
  717. switch ($borderAttributes["Style"]) {
  718. case '0' :
  719. $styleArray['style'] = PHPExcel_Style_Border::BORDER_NONE;
  720. break;
  721. case '1' :
  722. $styleArray['style'] = PHPExcel_Style_Border::BORDER_THIN;
  723. break;
  724. case '2' :
  725. $styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUM;
  726. break;
  727. case '4' :
  728. $styleArray['style'] = PHPExcel_Style_Border::BORDER_DASHED;
  729. break;
  730. case '5' :
  731. $styleArray['style'] = PHPExcel_Style_Border::BORDER_THICK;
  732. break;
  733. case '6' :
  734. $styleArray['style'] = PHPExcel_Style_Border::BORDER_DOUBLE;
  735. break;
  736. case '7' :
  737. $styleArray['style'] = PHPExcel_Style_Border::BORDER_DOTTED;
  738. break;
  739. case '9' :
  740. $styleArray['style'] = PHPExcel_Style_Border::BORDER_DASHDOT;
  741. break;
  742. case '10' :
  743. $styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHDOT;
  744. break;
  745. case '11' :
  746. $styleArray['style'] = PHPExcel_Style_Border::BORDER_DASHDOTDOT;
  747. break;
  748. case '12' :
  749. $styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
  750. break;
  751. case '13' :
  752. $styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHDOTDOT;
  753. break;
  754. case '3' :
  755. $styleArray['style'] = PHPExcel_Style_Border::BORDER_SLANTDASHDOT;
  756. break;
  757. case '8' :
  758. $styleArray['style'] = PHPExcel_Style_Border::BORDER_MEDIUMDASHED;
  759. break;
  760. }
  761. return $styleArray;
  762. }
  763. private function _parseRichText($is = '') {
  764. $value = new PHPExcel_RichText();
  765. $value->createText($is);
  766. return $value;
  767. }
  768. private static function _parseGnumericColour($gnmColour) {
  769. list($gnmR,$gnmG,$gnmB) = explode(':',$gnmColour);
  770. $gnmR = substr(str_pad($gnmR,4,'0',STR_PAD_RIGHT),0,2);
  771. $gnmG = substr(str_pad($gnmG,4,'0',STR_PAD_RIGHT),0,2);
  772. $gnmB = substr(str_pad($gnmB,4,'0',STR_PAD_RIGHT),0,2);
  773. $RGB = $gnmR.$gnmG.$gnmB;
  774. // echo 'Excel Colour: ',$RGB,'<br />';
  775. return $RGB;
  776. }
  777. }