PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Classes/PHPExcel/Reader/Excel2003XML.php

https://github.com/iGroup/PHPExcel
PHP | 804 lines | 545 code | 84 blank | 175 comment | 95 complexity | 7a481350ffc1bc37db9b81c0d4d58be2 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Reader
  23. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. /**
  36. * PHPExcel_Reader_Excel2003XML
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Reader
  40. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Reader_Excel2003XML extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  43. {
  44. /**
  45. * Formats
  46. *
  47. * @var array
  48. */
  49. private $_styles = array();
  50. /**
  51. * Character set used in the file
  52. *
  53. * @var string
  54. */
  55. private $_charSet = 'UTF-8';
  56. /**
  57. * Create a new PHPExcel_Reader_Excel2003XML
  58. */
  59. public function __construct() {
  60. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  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. // Office xmlns:o="urn:schemas-microsoft-com:office:office"
  72. // Excel xmlns:x="urn:schemas-microsoft-com:office:excel"
  73. // XML Spreadsheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  74. // Spreadsheet component xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet"
  75. // XML schema xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
  76. // XML data type xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
  77. // MS-persist recordset xmlns:rs="urn:schemas-microsoft-com:rowset"
  78. // Rowset xmlns:z="#RowsetSchema"
  79. //
  80. $signature = array(
  81. '<?xml version="1.0"',
  82. '<?mso-application progid="Excel.Sheet"?>'
  83. );
  84. // Check if file exists
  85. if (!file_exists($pFilename)) {
  86. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  87. }
  88. // Read sample data (first 2 KB will do)
  89. $fh = fopen($pFilename, 'r');
  90. $data = fread($fh, 2048);
  91. fclose($fh);
  92. $valid = true;
  93. foreach($signature as $match) {
  94. // every part of the signature must be present
  95. if (strpos($data, $match) === false) {
  96. $valid = false;
  97. break;
  98. }
  99. }
  100. // Retrieve charset encoding
  101. if(preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/um',$data,$matches)) {
  102. $this->_charSet = strtoupper($matches[1]);
  103. }
  104. // echo 'Character Set is ',$this->_charSet,'<br />';
  105. return $valid;
  106. }
  107. /**
  108. * Reads names of the worksheets from a file, without parsing the whole file to a PHPExcel object
  109. *
  110. * @param string $pFilename
  111. * @throws PHPExcel_Reader_Exception
  112. */
  113. public function listWorksheetNames($pFilename)
  114. {
  115. // Check if file exists
  116. if (!file_exists($pFilename)) {
  117. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  118. }
  119. if (!$this->canRead($pFilename)) {
  120. throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
  121. }
  122. $worksheetNames = array();
  123. $xml = simplexml_load_file($pFilename);
  124. $namespaces = $xml->getNamespaces(true);
  125. $xml_ss = $xml->children($namespaces['ss']);
  126. foreach($xml_ss->Worksheet as $worksheet) {
  127. $worksheet_ss = $worksheet->attributes($namespaces['ss']);
  128. $worksheetNames[] = self::_convertStringEncoding((string) $worksheet_ss['Name'],$this->_charSet);
  129. }
  130. return $worksheetNames;
  131. }
  132. /**
  133. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  134. *
  135. * @param string $pFilename
  136. * @throws PHPExcel_Reader_Exception
  137. */
  138. public function listWorksheetInfo($pFilename)
  139. {
  140. // Check if file exists
  141. if (!file_exists($pFilename)) {
  142. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  143. }
  144. $worksheetInfo = array();
  145. $xml = simplexml_load_file($pFilename);
  146. $namespaces = $xml->getNamespaces(true);
  147. $worksheetID = 1;
  148. $xml_ss = $xml->children($namespaces['ss']);
  149. foreach($xml_ss->Worksheet as $worksheet) {
  150. $worksheet_ss = $worksheet->attributes($namespaces['ss']);
  151. $tmpInfo = array();
  152. $tmpInfo['worksheetName'] = '';
  153. $tmpInfo['lastColumnLetter'] = 'A';
  154. $tmpInfo['lastColumnIndex'] = 0;
  155. $tmpInfo['totalRows'] = 0;
  156. $tmpInfo['totalColumns'] = 0;
  157. if (isset($worksheet_ss['Name'])) {
  158. $tmpInfo['worksheetName'] = (string) $worksheet_ss['Name'];
  159. } else {
  160. $tmpInfo['worksheetName'] = "Worksheet_{$worksheetID}";
  161. }
  162. if (isset($worksheet->Table->Row)) {
  163. $rowIndex = 0;
  164. foreach($worksheet->Table->Row as $rowData) {
  165. $columnIndex = 0;
  166. $rowHasData = false;
  167. foreach($rowData->Cell as $cell) {
  168. if (isset($cell->Data)) {
  169. $tmpInfo['lastColumnIndex'] = max($tmpInfo['lastColumnIndex'], $columnIndex);
  170. $rowHasData = true;
  171. }
  172. ++$columnIndex;
  173. }
  174. ++$rowIndex;
  175. if ($rowHasData) {
  176. $tmpInfo['totalRows'] = max($tmpInfo['totalRows'], $rowIndex);
  177. }
  178. }
  179. }
  180. $tmpInfo['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($tmpInfo['lastColumnIndex']);
  181. $tmpInfo['totalColumns'] = $tmpInfo['lastColumnIndex'] + 1;
  182. $worksheetInfo[] = $tmpInfo;
  183. ++$worksheetID;
  184. }
  185. return $worksheetInfo;
  186. }
  187. /**
  188. * Loads PHPExcel from file
  189. *
  190. * @param string $pFilename
  191. * @return PHPExcel
  192. * @throws PHPExcel_Reader_Exception
  193. */
  194. public function load($pFilename)
  195. {
  196. // Create new PHPExcel
  197. $objPHPExcel = new PHPExcel();
  198. // Load into this instance
  199. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  200. }
  201. private static function identifyFixedStyleValue($styleList,&$styleAttributeValue) {
  202. $styleAttributeValue = strtolower($styleAttributeValue);
  203. foreach($styleList as $style) {
  204. if ($styleAttributeValue == strtolower($style)) {
  205. $styleAttributeValue = $style;
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. /**
  212. * pixel units to excel width units(units of 1/256th of a character width)
  213. * @param pxs
  214. * @return
  215. */
  216. private static function _pixel2WidthUnits($pxs) {
  217. $UNIT_OFFSET_MAP = array(0, 36, 73, 109, 146, 182, 219);
  218. $widthUnits = 256 * ($pxs / 7);
  219. $widthUnits += $UNIT_OFFSET_MAP[($pxs % 7)];
  220. return $widthUnits;
  221. }
  222. /**
  223. * excel width units(units of 1/256th of a character width) to pixel units
  224. * @param widthUnits
  225. * @return
  226. */
  227. private static function _widthUnits2Pixel($widthUnits) {
  228. $pixels = ($widthUnits / 256) * 7;
  229. $offsetWidthUnits = $widthUnits % 256;
  230. $pixels += round($offsetWidthUnits / (256 / 7));
  231. return $pixels;
  232. }
  233. private static function _hex2str($hex) {
  234. return chr(hexdec($hex[1]));
  235. }
  236. /**
  237. * Loads PHPExcel from file into PHPExcel instance
  238. *
  239. * @param string $pFilename
  240. * @param PHPExcel $objPHPExcel
  241. * @return PHPExcel
  242. * @throws PHPExcel_Reader_Exception
  243. */
  244. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  245. {
  246. $fromFormats = array('\-', '\ ');
  247. $toFormats = array('-', ' ');
  248. $underlineStyles = array (
  249. PHPExcel_Style_Font::UNDERLINE_NONE,
  250. PHPExcel_Style_Font::UNDERLINE_DOUBLE,
  251. PHPExcel_Style_Font::UNDERLINE_DOUBLEACCOUNTING,
  252. PHPExcel_Style_Font::UNDERLINE_SINGLE,
  253. PHPExcel_Style_Font::UNDERLINE_SINGLEACCOUNTING
  254. );
  255. $verticalAlignmentStyles = array (
  256. PHPExcel_Style_Alignment::VERTICAL_BOTTOM,
  257. PHPExcel_Style_Alignment::VERTICAL_TOP,
  258. PHPExcel_Style_Alignment::VERTICAL_CENTER,
  259. PHPExcel_Style_Alignment::VERTICAL_JUSTIFY
  260. );
  261. $horizontalAlignmentStyles = array (
  262. PHPExcel_Style_Alignment::HORIZONTAL_GENERAL,
  263. PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
  264. PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
  265. PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  266. PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS,
  267. PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY
  268. );
  269. $timezoneObj = new DateTimeZone('Europe/London');
  270. $GMT = new DateTimeZone('UTC');
  271. // Check if file exists
  272. if (!file_exists($pFilename)) {
  273. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  274. }
  275. if (!$this->canRead($pFilename)) {
  276. throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
  277. }
  278. $xml = simplexml_load_file($pFilename);
  279. $namespaces = $xml->getNamespaces(true);
  280. $docProps = $objPHPExcel->getProperties();
  281. if (isset($xml->DocumentProperties[0])) {
  282. foreach($xml->DocumentProperties[0] as $propertyName => $propertyValue) {
  283. switch ($propertyName) {
  284. case 'Title' :
  285. $docProps->setTitle(self::_convertStringEncoding($propertyValue,$this->_charSet));
  286. break;
  287. case 'Subject' :
  288. $docProps->setSubject(self::_convertStringEncoding($propertyValue,$this->_charSet));
  289. break;
  290. case 'Author' :
  291. $docProps->setCreator(self::_convertStringEncoding($propertyValue,$this->_charSet));
  292. break;
  293. case 'Created' :
  294. $creationDate = strtotime($propertyValue);
  295. $docProps->setCreated($creationDate);
  296. break;
  297. case 'LastAuthor' :
  298. $docProps->setLastModifiedBy(self::_convertStringEncoding($propertyValue,$this->_charSet));
  299. break;
  300. case 'LastSaved' :
  301. $lastSaveDate = strtotime($propertyValue);
  302. $docProps->setModified($lastSaveDate);
  303. break;
  304. case 'Company' :
  305. $docProps->setCompany(self::_convertStringEncoding($propertyValue,$this->_charSet));
  306. break;
  307. case 'Category' :
  308. $docProps->setCategory(self::_convertStringEncoding($propertyValue,$this->_charSet));
  309. break;
  310. case 'Manager' :
  311. $docProps->setManager(self::_convertStringEncoding($propertyValue,$this->_charSet));
  312. break;
  313. case 'Keywords' :
  314. $docProps->setKeywords(self::_convertStringEncoding($propertyValue,$this->_charSet));
  315. break;
  316. case 'Description' :
  317. $docProps->setDescription(self::_convertStringEncoding($propertyValue,$this->_charSet));
  318. break;
  319. }
  320. }
  321. }
  322. if (isset($xml->CustomDocumentProperties)) {
  323. foreach($xml->CustomDocumentProperties[0] as $propertyName => $propertyValue) {
  324. $propertyAttributes = $propertyValue->attributes($namespaces['dt']);
  325. $propertyName = preg_replace_callback('/_x([0-9a-z]{4})_/','PHPExcel_Reader_Excel2003XML::_hex2str',$propertyName);
  326. $propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_UNKNOWN;
  327. switch((string) $propertyAttributes) {
  328. case 'string' :
  329. $propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_STRING;
  330. $propertyValue = trim($propertyValue);
  331. break;
  332. case 'boolean' :
  333. $propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_BOOLEAN;
  334. $propertyValue = (bool) $propertyValue;
  335. break;
  336. case 'integer' :
  337. $propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_INTEGER;
  338. $propertyValue = intval($propertyValue);
  339. break;
  340. case 'float' :
  341. $propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_FLOAT;
  342. $propertyValue = floatval($propertyValue);
  343. break;
  344. case 'dateTime.tz' :
  345. $propertyType = PHPExcel_DocumentProperties::PROPERTY_TYPE_DATE;
  346. $propertyValue = strtotime(trim($propertyValue));
  347. break;
  348. }
  349. $docProps->setCustomProperty($propertyName,$propertyValue,$propertyType);
  350. }
  351. }
  352. foreach($xml->Styles[0] as $style) {
  353. $style_ss = $style->attributes($namespaces['ss']);
  354. $styleID = (string) $style_ss['ID'];
  355. // echo 'Style ID = '.$styleID.'<br />';
  356. if ($styleID == 'Default') {
  357. $this->_styles['Default'] = array();
  358. } else {
  359. $this->_styles[$styleID] = $this->_styles['Default'];
  360. }
  361. foreach ($style as $styleType => $styleData) {
  362. $styleAttributes = $styleData->attributes($namespaces['ss']);
  363. // echo $styleType.'<br />';
  364. switch ($styleType) {
  365. case 'Alignment' :
  366. foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
  367. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  368. $styleAttributeValue = (string) $styleAttributeValue;
  369. switch ($styleAttributeKey) {
  370. case 'Vertical' :
  371. if (self::identifyFixedStyleValue($verticalAlignmentStyles,$styleAttributeValue)) {
  372. $this->_styles[$styleID]['alignment']['vertical'] = $styleAttributeValue;
  373. }
  374. break;
  375. case 'Horizontal' :
  376. if (self::identifyFixedStyleValue($horizontalAlignmentStyles,$styleAttributeValue)) {
  377. $this->_styles[$styleID]['alignment']['horizontal'] = $styleAttributeValue;
  378. }
  379. break;
  380. case 'WrapText' :
  381. $this->_styles[$styleID]['alignment']['wrap'] = true;
  382. break;
  383. }
  384. }
  385. break;
  386. case 'Borders' :
  387. foreach($styleData->Border as $borderStyle) {
  388. $borderAttributes = $borderStyle->attributes($namespaces['ss']);
  389. $thisBorder = array();
  390. foreach($borderAttributes as $borderStyleKey => $borderStyleValue) {
  391. // echo $borderStyleKey.' = '.$borderStyleValue.'<br />';
  392. switch ($borderStyleKey) {
  393. case 'LineStyle' :
  394. $thisBorder['style'] = PHPExcel_Style_Border::BORDER_MEDIUM;
  395. // $thisBorder['style'] = $borderStyleValue;
  396. break;
  397. case 'Weight' :
  398. // $thisBorder['style'] = $borderStyleValue;
  399. break;
  400. case 'Position' :
  401. $borderPosition = strtolower($borderStyleValue);
  402. break;
  403. case 'Color' :
  404. $borderColour = substr($borderStyleValue,1);
  405. $thisBorder['color']['rgb'] = $borderColour;
  406. break;
  407. }
  408. }
  409. if (!empty($thisBorder)) {
  410. if (($borderPosition == 'left') || ($borderPosition == 'right') || ($borderPosition == 'top') || ($borderPosition == 'bottom')) {
  411. $this->_styles[$styleID]['borders'][$borderPosition] = $thisBorder;
  412. }
  413. }
  414. }
  415. break;
  416. case 'Font' :
  417. foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
  418. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  419. $styleAttributeValue = (string) $styleAttributeValue;
  420. switch ($styleAttributeKey) {
  421. case 'FontName' :
  422. $this->_styles[$styleID]['font']['name'] = $styleAttributeValue;
  423. break;
  424. case 'Size' :
  425. $this->_styles[$styleID]['font']['size'] = $styleAttributeValue;
  426. break;
  427. case 'Color' :
  428. $this->_styles[$styleID]['font']['color']['rgb'] = substr($styleAttributeValue,1);
  429. break;
  430. case 'Bold' :
  431. $this->_styles[$styleID]['font']['bold'] = true;
  432. break;
  433. case 'Italic' :
  434. $this->_styles[$styleID]['font']['italic'] = true;
  435. break;
  436. case 'Underline' :
  437. if (self::identifyFixedStyleValue($underlineStyles,$styleAttributeValue)) {
  438. $this->_styles[$styleID]['font']['underline'] = $styleAttributeValue;
  439. }
  440. break;
  441. }
  442. }
  443. break;
  444. case 'Interior' :
  445. foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
  446. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  447. switch ($styleAttributeKey) {
  448. case 'Color' :
  449. $this->_styles[$styleID]['fill']['color']['rgb'] = substr($styleAttributeValue,1);
  450. break;
  451. }
  452. }
  453. break;
  454. case 'NumberFormat' :
  455. foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
  456. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  457. $styleAttributeValue = str_replace($fromFormats,$toFormats,$styleAttributeValue);
  458. switch ($styleAttributeValue) {
  459. case 'Short Date' :
  460. $styleAttributeValue = 'dd/mm/yyyy';
  461. break;
  462. }
  463. if ($styleAttributeValue > '') {
  464. $this->_styles[$styleID]['numberformat']['code'] = $styleAttributeValue;
  465. }
  466. }
  467. break;
  468. case 'Protection' :
  469. foreach($styleAttributes as $styleAttributeKey => $styleAttributeValue) {
  470. // echo $styleAttributeKey.' = '.$styleAttributeValue.'<br />';
  471. }
  472. break;
  473. }
  474. }
  475. // print_r($this->_styles[$styleID]);
  476. // echo '<hr />';
  477. }
  478. // echo '<hr />';
  479. $worksheetID = 0;
  480. $xml_ss = $xml->children($namespaces['ss']);
  481. foreach($xml_ss->Worksheet as $worksheet) {
  482. $worksheet_ss = $worksheet->attributes($namespaces['ss']);
  483. if ((isset($this->_loadSheetsOnly)) && (isset($worksheet_ss['Name'])) &&
  484. (!in_array($worksheet_ss['Name'], $this->_loadSheetsOnly))) {
  485. continue;
  486. }
  487. // echo '<h3>Worksheet: ',$worksheet_ss['Name'],'<h3>';
  488. //
  489. // Create new Worksheet
  490. $objPHPExcel->createSheet();
  491. $objPHPExcel->setActiveSheetIndex($worksheetID);
  492. if (isset($worksheet_ss['Name'])) {
  493. $worksheetName = self::_convertStringEncoding((string) $worksheet_ss['Name'],$this->_charSet);
  494. // Use false for $updateFormulaCellReferences to prevent adjustment of worksheet references in
  495. // formula cells... during the load, all formulae should be correct, and we're simply bringing
  496. // the worksheet name in line with the formula, not the reverse
  497. $objPHPExcel->getActiveSheet()->setTitle($worksheetName,false);
  498. }
  499. $columnID = 'A';
  500. if (isset($worksheet->Table->Column)) {
  501. foreach($worksheet->Table->Column as $columnData) {
  502. $columnData_ss = $columnData->attributes($namespaces['ss']);
  503. if (isset($columnData_ss['Index'])) {
  504. $columnID = PHPExcel_Cell::stringFromColumnIndex($columnData_ss['Index']-1);
  505. }
  506. if (isset($columnData_ss['Width'])) {
  507. $columnWidth = $columnData_ss['Width'];
  508. // echo '<b>Setting column width for '.$columnID.' to '.$columnWidth.'</b><br />';
  509. $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setWidth($columnWidth / 5.4);
  510. }
  511. ++$columnID;
  512. }
  513. }
  514. $rowID = 1;
  515. if (isset($worksheet->Table->Row)) {
  516. foreach($worksheet->Table->Row as $rowData) {
  517. $rowHasData = false;
  518. $row_ss = $rowData->attributes($namespaces['ss']);
  519. if (isset($row_ss['Index'])) {
  520. $rowID = (integer) $row_ss['Index'];
  521. }
  522. // echo '<b>Row '.$rowID.'</b><br />';
  523. $columnID = 'A';
  524. foreach($rowData->Cell as $cell) {
  525. $cell_ss = $cell->attributes($namespaces['ss']);
  526. if (isset($cell_ss['Index'])) {
  527. $columnID = PHPExcel_Cell::stringFromColumnIndex($cell_ss['Index']-1);
  528. }
  529. $cellRange = $columnID.$rowID;
  530. if ($this->getReadFilter() !== NULL) {
  531. if (!$this->getReadFilter()->readCell($columnID, $rowID, $worksheetName)) {
  532. continue;
  533. }
  534. }
  535. if ((isset($cell_ss['MergeAcross'])) || (isset($cell_ss['MergeDown']))) {
  536. $columnTo = $columnID;
  537. if (isset($cell_ss['MergeAcross'])) {
  538. $columnTo = PHPExcel_Cell::stringFromColumnIndex(PHPExcel_Cell::columnIndexFromString($columnID) + $cell_ss['MergeAcross'] -1);
  539. }
  540. $rowTo = $rowID;
  541. if (isset($cell_ss['MergeDown'])) {
  542. $rowTo = $rowTo + $cell_ss['MergeDown'];
  543. }
  544. $cellRange .= ':'.$columnTo.$rowTo;
  545. $objPHPExcel->getActiveSheet()->mergeCells($cellRange);
  546. }
  547. $cellIsSet = $hasCalculatedValue = false;
  548. $cellDataFormula = '';
  549. if (isset($cell_ss['Formula'])) {
  550. $cellDataFormula = $cell_ss['Formula'];
  551. // added this as a check for array formulas
  552. if (isset($cell_ss['ArrayRange'])) {
  553. $cellDataCSEFormula = $cell_ss['ArrayRange'];
  554. // echo "found an array formula at ".$columnID.$rowID."<br />";
  555. }
  556. $hasCalculatedValue = true;
  557. }
  558. if (isset($cell->Data)) {
  559. $cellValue = $cellData = $cell->Data;
  560. $type = PHPExcel_Cell_DataType::TYPE_NULL;
  561. $cellData_ss = $cellData->attributes($namespaces['ss']);
  562. if (isset($cellData_ss['Type'])) {
  563. $cellDataType = $cellData_ss['Type'];
  564. switch ($cellDataType) {
  565. /*
  566. const TYPE_STRING = 's';
  567. const TYPE_FORMULA = 'f';
  568. const TYPE_NUMERIC = 'n';
  569. const TYPE_BOOL = 'b';
  570. const TYPE_NULL = 'null';
  571. const TYPE_INLINE = 'inlineStr';
  572. const TYPE_ERROR = 'e';
  573. */
  574. case 'String' :
  575. $cellValue = self::_convertStringEncoding($cellValue,$this->_charSet);
  576. $type = PHPExcel_Cell_DataType::TYPE_STRING;
  577. break;
  578. case 'Number' :
  579. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  580. $cellValue = (float) $cellValue;
  581. if (floor($cellValue) == $cellValue) {
  582. $cellValue = (integer) $cellValue;
  583. }
  584. break;
  585. case 'Boolean' :
  586. $type = PHPExcel_Cell_DataType::TYPE_BOOL;
  587. $cellValue = ($cellValue != 0);
  588. break;
  589. case 'DateTime' :
  590. $type = PHPExcel_Cell_DataType::TYPE_NUMERIC;
  591. $cellValue = PHPExcel_Shared_Date::PHPToExcel(strtotime($cellValue));
  592. break;
  593. case 'Error' :
  594. $type = PHPExcel_Cell_DataType::TYPE_ERROR;
  595. break;
  596. }
  597. }
  598. if ($hasCalculatedValue) {
  599. // echo 'FORMULA<br />';
  600. $type = PHPExcel_Cell_DataType::TYPE_FORMULA;
  601. $columnNumber = PHPExcel_Cell::columnIndexFromString($columnID);
  602. if (substr($cellDataFormula,0,3) == 'of:') {
  603. $cellDataFormula = substr($cellDataFormula,3);
  604. // echo 'Before: ',$cellDataFormula,'<br />';
  605. $temp = explode('"',$cellDataFormula);
  606. $key = false;
  607. foreach($temp as &$value) {
  608. // Only replace in alternate array entries (i.e. non-quoted blocks)
  609. if ($key = !$key) {
  610. $value = str_replace(array('[.','.',']'),'',$value);
  611. }
  612. }
  613. } else {
  614. // Convert R1C1 style references to A1 style references (but only when not quoted)
  615. // echo 'Before: ',$cellDataFormula,'<br />';
  616. $temp = explode('"',$cellDataFormula);
  617. $key = false;
  618. foreach($temp as &$value) {
  619. // Only replace in alternate array entries (i.e. non-quoted blocks)
  620. if ($key = !$key) {
  621. preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
  622. // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
  623. // through the formula from left to right. Reversing means that we work right to left.through
  624. // the formula
  625. $cellReferences = array_reverse($cellReferences);
  626. // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
  627. // then modify the formula to use that new reference
  628. foreach($cellReferences as $cellReference) {
  629. $rowReference = $cellReference[2][0];
  630. // Empty R reference is the current row
  631. if ($rowReference == '') $rowReference = $rowID;
  632. // Bracketed R references are relative to the current row
  633. if ($rowReference{0} == '[') $rowReference = $rowID + trim($rowReference,'[]');
  634. $columnReference = $cellReference[4][0];
  635. // Empty C reference is the current column
  636. if ($columnReference == '') $columnReference = $columnNumber;
  637. // Bracketed C references are relative to the current column
  638. if ($columnReference{0} == '[') $columnReference = $columnNumber + trim($columnReference,'[]');
  639. $A1CellReference = PHPExcel_Cell::stringFromColumnIndex($columnReference-1).$rowReference;
  640. $value = substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
  641. }
  642. }
  643. }
  644. }
  645. unset($value);
  646. // Then rebuild the formula string
  647. $cellDataFormula = implode('"',$temp);
  648. // echo 'After: ',$cellDataFormula,'<br />';
  649. }
  650. // echo 'Cell '.$columnID.$rowID.' is a '.$type.' with a value of '.(($hasCalculatedValue) ? $cellDataFormula : $cellValue).'<br />';
  651. //
  652. $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue),$type);
  653. if ($hasCalculatedValue) {
  654. // echo 'Formula result is '.$cellValue.'<br />';
  655. $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setCalculatedValue($cellValue);
  656. }
  657. $cellIsSet = $rowHasData = true;
  658. }
  659. if (isset($cell->Comment)) {
  660. // echo '<b>comment found</b><br />';
  661. $commentAttributes = $cell->Comment->attributes($namespaces['ss']);
  662. $author = 'unknown';
  663. if (isset($commentAttributes->Author)) {
  664. $author = (string)$commentAttributes->Author;
  665. // echo 'Author: ',$author,'<br />';
  666. }
  667. $node = $cell->Comment->Data->asXML();
  668. // $annotation = str_replace('html:','',substr($node,49,-10));
  669. // echo $annotation,'<br />';
  670. $annotation = strip_tags($node);
  671. // echo 'Annotation: ',$annotation,'<br />';
  672. $objPHPExcel->getActiveSheet()->getComment( $columnID.$rowID )
  673. ->setAuthor(self::_convertStringEncoding($author ,$this->_charSet))
  674. ->setText($this->_parseRichText($annotation) );
  675. }
  676. if (($cellIsSet) && (isset($cell_ss['StyleID']))) {
  677. $style = (string) $cell_ss['StyleID'];
  678. // echo 'Cell style for '.$columnID.$rowID.' is '.$style.'<br />';
  679. if ((isset($this->_styles[$style])) && (!empty($this->_styles[$style]))) {
  680. // echo 'Cell '.$columnID.$rowID.'<br />';
  681. // print_r($this->_styles[$style]);
  682. // echo '<br />';
  683. if (!$objPHPExcel->getActiveSheet()->cellExists($columnID.$rowID)) {
  684. $objPHPExcel->getActiveSheet()->getCell($columnID.$rowID)->setValue(NULL);
  685. }
  686. $objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($this->_styles[$style]);
  687. }
  688. }
  689. ++$columnID;
  690. }
  691. if ($rowHasData) {
  692. if (isset($row_ss['StyleID'])) {
  693. $rowStyle = $row_ss['StyleID'];
  694. }
  695. if (isset($row_ss['Height'])) {
  696. $rowHeight = $row_ss['Height'];
  697. // echo '<b>Setting row height to '.$rowHeight.'</b><br />';
  698. $objPHPExcel->getActiveSheet()->getRowDimension($rowID)->setRowHeight($rowHeight);
  699. }
  700. }
  701. ++$rowID;
  702. }
  703. }
  704. ++$worksheetID;
  705. }
  706. // Return
  707. return $objPHPExcel;
  708. }
  709. private static function _convertStringEncoding($string,$charset) {
  710. if ($charset != 'UTF-8') {
  711. return PHPExcel_Shared_String::ConvertEncoding($string,'UTF-8',$charset);
  712. }
  713. return $string;
  714. }
  715. private function _parseRichText($is = '') {
  716. $value = new PHPExcel_RichText();
  717. $value->createText(self::_convertStringEncoding($is,$this->_charSet));
  718. return $value;
  719. }
  720. }