OOCalc.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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_OOCalc
  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_OOCalc extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  40. {
  41. /**
  42. * Formats
  43. *
  44. * @var array
  45. */
  46. private $styles = array();
  47. /**
  48. * Create a new PHPExcel_Reader_OOCalc
  49. */
  50. public function __construct()
  51. {
  52. $this->readFilter = new PHPExcel_Reader_DefaultReadFilter();
  53. }
  54. /**
  55. * Can the current PHPExcel_Reader_IReader read the file?
  56. *
  57. * @param string $pFilename
  58. * @return boolean
  59. * @throws PHPExcel_Reader_Exception
  60. */
  61. public function canRead($pFilename)
  62. {
  63. // Check if file exists
  64. if (!file_exists($pFilename)) {
  65. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  66. }
  67. $zipClass = PHPExcel_Settings::getZipClass();
  68. // Check if zip class exists
  69. // if (!class_exists($zipClass, false)) {
  70. // throw new PHPExcel_Reader_Exception($zipClass . " library is not enabled");
  71. // }
  72. $mimeType = 'UNKNOWN';
  73. // Load file
  74. $zip = new $zipClass;
  75. if ($zip->open($pFilename) === true) {
  76. // check if it is an OOXML archive
  77. $stat = $zip->statName('mimetype');
  78. if ($stat && ($stat['size'] <= 255)) {
  79. $mimeType = $zip->getFromName($stat['name']);
  80. } elseif ($stat = $zip->statName('META-INF/manifest.xml')) {
  81. $xml = simplexml_load_string($this->securityScan($zip->getFromName('META-INF/manifest.xml')), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
  82. $namespacesContent = $xml->getNamespaces(true);
  83. if (isset($namespacesContent['manifest'])) {
  84. $manifest = $xml->children($namespacesContent['manifest']);
  85. foreach ($manifest as $manifestDataSet) {
  86. $manifestAttributes = $manifestDataSet->attributes($namespacesContent['manifest']);
  87. if ($manifestAttributes->{'full-path'} == '/') {
  88. $mimeType = (string) $manifestAttributes->{'media-type'};
  89. break;
  90. }
  91. }
  92. }
  93. }
  94. $zip->close();
  95. return ($mimeType === 'application/vnd.oasis.opendocument.spreadsheet');
  96. }
  97. return false;
  98. }
  99. /**
  100. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  101. *
  102. * @param string $pFilename
  103. * @throws PHPExcel_Reader_Exception
  104. */
  105. public function listWorksheetNames($pFilename)
  106. {
  107. // Check if file exists
  108. if (!file_exists($pFilename)) {
  109. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  110. }
  111. $zipClass = PHPExcel_Settings::getZipClass();
  112. $zip = new $zipClass;
  113. if (!$zip->open($pFilename)) {
  114. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
  115. }
  116. $worksheetNames = array();
  117. $xml = new XMLReader();
  118. $res = $xml->xml($this->securityScanFile('zip://'.realpath($pFilename).'#content.xml'), null, PHPExcel_Settings::getLibXmlLoaderOptions());
  119. $xml->setParserProperty(2, true);
  120. // Step into the first level of content of the XML
  121. $xml->read();
  122. while ($xml->read()) {
  123. // Quickly jump through to the office:body node
  124. while ($xml->name !== 'office:body') {
  125. if ($xml->isEmptyElement) {
  126. $xml->read();
  127. } else {
  128. $xml->next();
  129. }
  130. }
  131. // Now read each node until we find our first table:table node
  132. while ($xml->read()) {
  133. if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
  134. // Loop through each table:table node reading the table:name attribute for each worksheet name
  135. do {
  136. $worksheetNames[] = $xml->getAttribute('table:name');
  137. $xml->next();
  138. } while ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT);
  139. }
  140. }
  141. }
  142. return $worksheetNames;
  143. }
  144. /**
  145. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  146. *
  147. * @param string $pFilename
  148. * @throws PHPExcel_Reader_Exception
  149. */
  150. public function listWorksheetInfo($pFilename)
  151. {
  152. // Check if file exists
  153. if (!file_exists($pFilename)) {
  154. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  155. }
  156. $worksheetInfo = array();
  157. $zipClass = PHPExcel_Settings::getZipClass();
  158. $zip = new $zipClass;
  159. if (!$zip->open($pFilename)) {
  160. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
  161. }
  162. $xml = new XMLReader();
  163. $res = $xml->xml($this->securityScanFile('zip://'.realpath($pFilename).'#content.xml'), null, PHPExcel_Settings::getLibXmlLoaderOptions());
  164. $xml->setParserProperty(2, true);
  165. // Step into the first level of content of the XML
  166. $xml->read();
  167. while ($xml->read()) {
  168. // Quickly jump through to the office:body node
  169. while ($xml->name !== 'office:body') {
  170. if ($xml->isEmptyElement) {
  171. $xml->read();
  172. } else {
  173. $xml->next();
  174. }
  175. }
  176. // Now read each node until we find our first table:table node
  177. while ($xml->read()) {
  178. if ($xml->name == 'table:table' && $xml->nodeType == XMLReader::ELEMENT) {
  179. $worksheetNames[] = $xml->getAttribute('table:name');
  180. $tmpInfo = array(
  181. 'worksheetName' => $xml->getAttribute('table:name'),
  182. 'lastColumnLetter' => 'A',
  183. 'lastColumnIndex' => 0,
  184. 'totalRows' => 0,
  185. 'totalColumns' => 0,
  186. );
  187. // Loop through each child node of the table:table element reading
  188. $currCells = 0;
  189. do {
  190. $xml->read();
  191. if ($xml->name == 'table:table-row' && $xml->nodeType == XMLReader::ELEMENT) {
  192. $rowspan = $xml->getAttribute('table:number-rows-repeated');
  193. $rowspan = empty($rowspan) ? 1 : $rowspan;
  194. $tmpInfo['totalRows'] += $rowspan;
  195. $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
  196. $currCells = 0;
  197. // Step into the row
  198. $xml->read();
  199. do {
  200. if ($xml->name == 'table:table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
  201. if (!$xml->isEmptyElement) {
  202. $currCells++;
  203. $xml->next();
  204. } else {
  205. $xml->read();
  206. }
  207. } elseif ($xml->name == 'table:covered-table-cell' && $xml->nodeType == XMLReader::ELEMENT) {
  208. $mergeSize = $xml->getAttribute('table:number-columns-repeated');
  209. $currCells += $mergeSize;
  210. $xml->read();
  211. }
  212. } while ($xml->name != 'table:table-row');
  213. }
  214. } while ($xml->name != 'table:table');
  215. $tmpInfo['totalColumns'] = max($tmpInfo['totalColumns'], $currCells);
  216. $tmpInfo['lastColumnIndex'] = $tmpInfo['totalColumns'] - 1;
  217. $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
  218. $worksheetInfo[] = $tmpInfo;
  219. }
  220. }
  221. // foreach ($workbookData->table as $worksheetDataSet) {
  222. // $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
  223. // $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
  224. //
  225. // $rowIndex = 0;
  226. // foreach ($worksheetData as $key => $rowData) {
  227. // switch ($key) {
  228. // case 'table-row' :
  229. // $rowDataTableAttributes = $rowData->attributes($namespacesContent['table']);
  230. // $rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ?
  231. // $rowDataTableAttributes['number-rows-repeated'] : 1;
  232. // $columnIndex = 0;
  233. //
  234. // foreach ($rowData as $key => $cellData) {
  235. // $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
  236. // $colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ?
  237. // $cellDataTableAttributes['number-columns-repeated'] : 1;
  238. // $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
  239. // if (isset($cellDataOfficeAttributes['value-type'])) {
  240. // $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex + $colRepeats - 1);
  241. // $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex + $rowRepeats);
  242. // }
  243. // $columnIndex += $colRepeats;
  244. // }
  245. // $rowIndex += $rowRepeats;
  246. // break;
  247. // }
  248. // }
  249. //
  250. // $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
  251. // $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
  252. //
  253. // }
  254. // }
  255. }
  256. return $worksheetInfo;
  257. }
  258. /**
  259. * Loads PHPExcel from file
  260. *
  261. * @param string $pFilename
  262. * @return PHPExcel
  263. * @throws PHPExcel_Reader_Exception
  264. */
  265. public function load($pFilename)
  266. {
  267. // Create new PHPExcel
  268. $objPHPExcel = new PHPExcel();
  269. // Load into this instance
  270. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  271. }
  272. private static function identifyFixedStyleValue($styleList, &$styleAttributeValue)
  273. {
  274. $styleAttributeValue = strtolower($styleAttributeValue);
  275. foreach ($styleList as $style) {
  276. if ($styleAttributeValue == strtolower($style)) {
  277. $styleAttributeValue = $style;
  278. return true;
  279. }
  280. }
  281. return false;
  282. }
  283. /**
  284. * Loads PHPExcel from file into PHPExcel instance
  285. *
  286. * @param string $pFilename
  287. * @param PHPExcel $objPHPExcel
  288. * @return PHPExcel
  289. * @throws PHPExcel_Reader_Exception
  290. */
  291. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  292. {
  293. // Check if file exists
  294. if (!file_exists($pFilename)) {
  295. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  296. }
  297. $timezoneObj = new DateTimeZone('Europe/London');
  298. $GMT = new DateTimeZone('UTC');
  299. $zipClass = PHPExcel_Settings::getZipClass();
  300. $zip = new $zipClass;
  301. if (!$zip->open($pFilename)) {
  302. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! Error opening file.");
  303. }
  304. // echo '<h1>Meta Information</h1>';
  305. $xml = simplexml_load_string($this->securityScan($zip->getFromName("meta.xml")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
  306. $namespacesMeta = $xml->getNamespaces(true);
  307. // echo '<pre>';
  308. // print_r($namespacesMeta);
  309. // echo '</pre><hr />';
  310. $docProps = $objPHPExcel->getProperties();
  311. $officeProperty = $xml->children($namespacesMeta['office']);
  312. foreach ($officeProperty as $officePropertyData) {
  313. $officePropertyDC = array();
  314. if (isset($namespacesMeta['dc'])) {
  315. $officePropertyDC = $officePropertyData->children($namespacesMeta['dc']);
  316. }
  317. foreach ($officePropertyDC as $propertyName => $propertyValue) {
  318. $propertyValue = (string) $propertyValue;
  319. switch ($propertyName) {
  320. case 'title':
  321. $docProps->setTitle($propertyValue);
  322. break;
  323. case 'subject':
  324. $docProps->setSubject($propertyValue);
  325. break;
  326. case 'creator':
  327. $docProps->setCreator($propertyValue);
  328. $docProps->setLastModifiedBy($propertyValue);
  329. break;
  330. case 'date':
  331. $creationDate = strtotime($propertyValue);
  332. $docProps->setCreated($creationDate);
  333. $docProps->setModified($creationDate);
  334. break;
  335. case 'description':
  336. $docProps->setDescription($propertyValue);
  337. break;
  338. }
  339. }
  340. $officePropertyMeta = array();
  341. if (isset($namespacesMeta['dc'])) {
  342. $officePropertyMeta = $officePropertyData->children($namespacesMeta['meta']);
  343. }
  344. foreach ($officePropertyMeta as $propertyName => $propertyValue) {
  345. $propertyValueAttributes = $propertyValue->attributes($namespacesMeta['meta']);
  346. $propertyValue = (string) $propertyValue;
  347. switch ($propertyName) {
  348. case 'initial-creator':
  349. $docProps->setCreator($propertyValue);
  350. break;
  351. case 'keyword':
  352. $docProps->setKeywords($propertyValue);
  353. break;
  354. case 'creation-date':
  355. $creationDate = strtotime($propertyValue);
  356. $docProps->setCreated($creationDate);
  357. break;
  358. case 'user-defined':
  359. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
  360. foreach ($propertyValueAttributes as $key => $value) {
  361. if ($key == 'name') {
  362. $propertyValueName = (string) $value;
  363. } elseif ($key == 'value-type') {
  364. switch ($value) {
  365. case 'date':
  366. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue, 'date');
  367. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_DATE;
  368. break;
  369. case 'boolean':
  370. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue, 'bool');
  371. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_BOOLEAN;
  372. break;
  373. case 'float':
  374. $propertyValue = PHPExcel_DocumentProperties::convertProperty($propertyValue, 'r4');
  375. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_FLOAT;
  376. break;
  377. default:
  378. $propertyValueType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
  379. }
  380. }
  381. }
  382. $docProps->setCustomProperty($propertyValueName, $propertyValue, $propertyValueType);
  383. break;
  384. }
  385. }
  386. }
  387. // echo '<h1>Workbook Content</h1>';
  388. $xml = simplexml_load_string($this->securityScan($zip->getFromName("content.xml")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());
  389. $namespacesContent = $xml->getNamespaces(true);
  390. // echo '<pre>';
  391. // print_r($namespacesContent);
  392. // echo '</pre><hr />';
  393. $workbook = $xml->children($namespacesContent['office']);
  394. foreach ($workbook->body->spreadsheet as $workbookData) {
  395. $workbookData = $workbookData->children($namespacesContent['table']);
  396. $worksheetID = 0;
  397. foreach ($workbookData->table as $worksheetDataSet) {
  398. $worksheetData = $worksheetDataSet->children($namespacesContent['table']);
  399. // print_r($worksheetData);
  400. // echo '<br />';
  401. $worksheetDataAttributes = $worksheetDataSet->attributes($namespacesContent['table']);
  402. // print_r($worksheetDataAttributes);
  403. // echo '<br />';
  404. if ((isset($this->loadSheetsOnly)) && (isset($worksheetDataAttributes['name'])) &&
  405. (!in_array($worksheetDataAttributes['name'], $this->loadSheetsOnly))) {
  406. continue;
  407. }
  408. // echo '<h2>Worksheet '.$worksheetDataAttributes['name'].'</h2>';
  409. // Create new Worksheet
  410. $objPHPExcel->createSheet();
  411. $objPHPExcel->setActiveSheetIndex($worksheetID);
  412. if (isset($worksheetDataAttributes['name'])) {
  413. $worksheetName = (string) $worksheetDataAttributes['name'];
  414. // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
  415. // formula cells... during the load, all formulae should be correct, and we're simply
  416. // bringing the worksheet name in line with the formula, not the reverse
  417. $objPHPExcel->getActiveSheet()->setTitle($worksheetName, false);
  418. }
  419. $rowID = 1;
  420. foreach ($worksheetData as $key => $rowData) {
  421. // echo '<b>'.$key.'</b><br />';
  422. switch ($key) {
  423. case 'table-header-rows':
  424. foreach ($rowData as $key => $cellData) {
  425. $rowData = $cellData;
  426. break;
  427. }
  428. case 'table-row':
  429. $rowDataTableAttributes = $rowData->attributes($namespacesContent['table']);
  430. $rowRepeats = (isset($rowDataTableAttributes['number-rows-repeated'])) ? $rowDataTableAttributes['number-rows-repeated'] : 1;
  431. $columnID = 'A';
  432. foreach ($rowData as $key => $cellData) {
  433. if ($this->getReadFilter() !== null) {
  434. if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
  435. continue;
  436. }
  437. }
  438. // echo '<b>'.$columnID.$rowID.'</b><br />';
  439. $cellDataText = (isset($namespacesContent['text'])) ? $cellData->children($namespacesContent['text']) : '';
  440. $cellDataOffice = $cellData->children($namespacesContent['office']);
  441. $cellDataOfficeAttributes = $cellData->attributes($namespacesContent['office']);
  442. $cellDataTableAttributes = $cellData->attributes($namespacesContent['table']);
  443. // echo 'Office Attributes: ';
  444. // print_r($cellDataOfficeAttributes);
  445. // echo '<br />Table Attributes: ';
  446. // print_r($cellDataTableAttributes);
  447. // echo '<br />Cell Data Text';
  448. // print_r($cellDataText);
  449. // echo '<br />';
  450. //
  451. $type = $formatting = $hyperlink = null;
  452. $hasCalculatedValue = false;
  453. $cellDataFormula = '';
  454. if (isset($cellDataTableAttributes['formula'])) {
  455. $cellDataFormula = $cellDataTableAttributes['formula'];
  456. $hasCalculatedValue = true;
  457. }
  458. if (isset($cellDataOffice->annotation)) {
  459. // echo 'Cell has comment<br />';
  460. $annotationText = $cellDataOffice->annotation->children($namespacesContent['text']);
  461. $textArray = array();
  462. foreach ($annotationText as $t) {
  463. if (isset($t->span)) {
  464. foreach ($t->span as $text) {
  465. $textArray[] = (string)$text;
  466. }
  467. } else {
  468. $textArray[] = (string) $t;
  469. }
  470. }
  471. $text = implode("\n", $textArray);
  472. // echo $text, '<br />';
  473. $objPHPExcel->getActiveSheet()->getComment($columnID.$rowID)->setText($this->parseRichText($text));
  474. // ->setAuthor( $author )
  475. }
  476. if (isset($cellDataText->p)) {
  477. // Consolidate if there are multiple p records (maybe with spans as well)
  478. $dataArray = array();
  479. // Text can have multiple text:p and within those, multiple text:span.
  480. // text:p newlines, but text:span does not.
  481. // Also, here we assume there is no text data is span fields are specified, since
  482. // we have no way of knowing proper positioning anyway.
  483. foreach ($cellDataText->p as $pData) {
  484. if (isset($pData->span)) {
  485. // span sections do not newline, so we just create one large string here
  486. $spanSection = "";
  487. foreach ($pData->span as $spanData) {
  488. $spanSection .= $spanData;
  489. }
  490. array_push($dataArray, $spanSection);
  491. } else {
  492. array_push($dataArray, $pData);
  493. }
  494. }
  495. $allCellDataText = implode($dataArray, "\n");
  496. // echo 'Value Type is '.$cellDataOfficeAttributes['value-type'].'<br />';
  497. switch ($cellDataOfficeAttributes['value-type']) {
  498. case 'string':
  499. $type = PHPExcel_Cell_DataType::TYPE_STRING;
  500. $dataValue = $allCellDataText;
  501. if (isset($dataValue->a)) {
  502. $dataValue = $dataValue->a;
  503. $cellXLinkAttributes = $dataValue->attributes($namespacesContent['xlink']);
  504. $hyperlink = $cellXLinkAttributes['href'];
  505. }
  506. break;
  507. case 'boolean':
  508. $type = PHPExcel_Cell_DataType::TYPE_BOOL;
  509. $dataValue = ($allCellDataText == 'TRUE') ? true : false;
  510. break;
  511. case 'percentage':
  512. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  513. $dataValue = (float) $cellDataOfficeAttributes['value'];
  514. if (floor($dataValue) == $dataValue) {
  515. $dataValue = (integer) $dataValue;
  516. }
  517. $formatting = PHPExcel_Style_NumberFormat::FORMAT_PERCENTAGE_00;
  518. break;
  519. case 'currency':
  520. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  521. $dataValue = (float) $cellDataOfficeAttributes['value'];
  522. if (floor($dataValue) == $dataValue) {
  523. $dataValue = (integer) $dataValue;
  524. }
  525. $formatting = PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
  526. break;
  527. case 'float':
  528. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  529. $dataValue = (float) $cellDataOfficeAttributes['value'];
  530. if (floor($dataValue) == $dataValue) {
  531. if ($dataValue == (integer) $dataValue) {
  532. $dataValue = (integer) $dataValue;
  533. } else {
  534. $dataValue = (float) $dataValue;
  535. }
  536. }
  537. break;
  538. case 'date':
  539. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  540. $dateObj = new DateTime($cellDataOfficeAttributes['date-value'], $GMT);
  541. $dateObj->setTimeZone($timezoneObj);
  542. list($year, $month, $day, $hour, $minute, $second) = explode(' ', $dateObj->format('Y m d H i s'));
  543. $dataValue = PHPExcel_Shared_Date::FormattedPHPToExcel($year, $month, $day, $hour, $minute, $second);
  544. if ($dataValue != floor($dataValue)) {
  545. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15.' '.PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  546. } else {
  547. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_XLSX15;
  548. }
  549. break;
  550. case 'time':
  551. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  552. $dataValue = PHPExcel_Shared_Date::PHPToExcel(strtotime('01-01-1970 '.implode(':', sscanf($cellDataOfficeAttributes['time-value'], 'PT%dH%dM%dS'))));
  553. $formatting = PHPExcel_Style_NumberFormat::FORMAT_DATE_TIME4;
  554. break;
  555. }
  556. // echo 'Data value is '.$dataValue.'<br />';
  557. // if ($hyperlink !== null) {
  558. // echo 'Hyperlink is '.$hyperlink.'<br />';
  559. // }
  560. } else {
  561. $type = PHPExcel_Cell_DataType::TYPE_NULL;
  562. $dataValue = null;
  563. }
  564. if ($hasCalculatedValue) {
  565. $type = PHPExcel_Cell_DataType::TYPE_FORMULA;
  566. // echo 'Formula: ', $cellDataFormula, PHP_EOL;
  567. $cellDataFormula = substr($cellDataFormula, strpos($cellDataFormula, ':=')+1);
  568. $temp = explode('"', $cellDataFormula);
  569. $tKey = false;
  570. foreach ($temp as &$value) {
  571. // Only replace in alternate array entries (i.e. non-quoted blocks)
  572. if ($tKey = !$tKey) {
  573. $value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/Ui', '$1!$2:$3', $value); // Cell range reference in another sheet
  574. $value = preg_replace('/\[([^\.]+)\.([^\.]+)\]/Ui', '$1!$2', $value); // Cell reference in another sheet
  575. $value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/Ui', '$1:$2', $value); // Cell range reference
  576. $value = preg_replace('/\[\.([^\.]+)\]/Ui', '$1', $value); // Simple cell reference
  577. $value = PHPExcel_Calculation::translateSeparator(';', ',', $value, $inBraces);
  578. }
  579. }
  580. unset($value);
  581. // Then rebuild the formula string
  582. $cellDataFormula = implode('"', $temp);
  583. // echo 'Adjusted Formula: ', $cellDataFormula, PHP_EOL;
  584. }
  585. $colRepeats = (isset($cellDataTableAttributes['number-columns-repeated'])) ? $cellDataTableAttributes['number-columns-repeated'] : 1;
  586. if ($type !== null) {
  587. for ($i = 0; $i < $colRepeats; ++$i) {
  588. if ($i > 0) {
  589. ++$columnID;
  590. }
  591. if ($type !== PHPExcel_Cell_DataType::TYPE_NULL) {
  592. for ($rowAdjust = 0; $rowAdjust < $rowRepeats; ++$rowAdjust) {
  593. $rID = $rowID + $rowAdjust;
  594. $objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $dataValue), $type);
  595. if ($hasCalculatedValue) {
  596. // echo 'Forumla result is '.$dataValue.'<br />';
  597. $objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->setCalculatedValue($dataValue);
  598. }
  599. if ($formatting !== null) {
  600. $objPHPExcel->getActiveSheet()->getStyle($columnID.$rID)->getNumberFormat()->setFormatCode($formatting);
  601. } else {
  602. $objPHPExcel->getActiveSheet()->getStyle($columnID.$rID)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
  603. }
  604. if ($hyperlink !== null) {
  605. $objPHPExcel->getActiveSheet()->getCell($columnID.$rID)->getHyperlink()->setUrl($hyperlink);
  606. }
  607. }
  608. }
  609. }
  610. }
  611. // Merged cells
  612. if ((isset($cellDataTableAttributes['number-columns-spanned'])) || (isset($cellDataTableAttributes['number-rows-spanned']))) {
  613. if (($type !== PHPExcel_Cell_DataType::TYPE_NULL) || (!$this->readDataOnly)) {
  614. $columnTo = $columnID;
  615. if (isset($cellDataTableAttributes['number-columns-spanned'])) {
  616. $columnTo = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID) + $cellDataTableAttributes['number-columns-spanned'] -2);
  617. }
  618. $rowTo = $rowID;
  619. if (isset($cellDataTableAttributes['number-rows-spanned'])) {
  620. $rowTo = $rowTo + $cellDataTableAttributes['number-rows-spanned'] - 1;
  621. }
  622. $cellRange = $columnID.$rowID.':'.$columnTo.$rowTo;
  623. $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
  624. }
  625. }
  626. ++$columnID;
  627. }
  628. $rowID += $rowRepeats;
  629. break;
  630. }
  631. }
  632. ++$worksheetID;
  633. }
  634. }
  635. // Return
  636. return $objPHPExcel;
  637. }
  638. private function parseRichText($is = '')
  639. {
  640. $value = new PHPExcel_RichText();
  641. $value->createText($is);
  642. return $value;
  643. }
  644. }