No Description

Excel2003XML.php 28KB

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