Excel2003XML.php 27 KB

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