Gnumeric.php 42 KB

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