Gnumeric.php 30 KB

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