PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/PHPExcel/PHPExcel/Reader/SYLK.php

https://bitbucket.org/kylestlb/cse360site
PHP | 415 lines | 232 code | 35 blank | 148 comment | 43 complexity | 9f45292d8392b668e2e2fa689f62728e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  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_SYLK
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Reader
  40. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
  43. {
  44. /**
  45. * Input encoding
  46. *
  47. * @var string
  48. */
  49. private $_inputEncoding = 'ANSI';
  50. /**
  51. * Sheet index to read
  52. *
  53. * @var int
  54. */
  55. private $_sheetIndex = 0;
  56. /**
  57. * Formats
  58. *
  59. * @var array
  60. */
  61. private $_formats = array();
  62. /**
  63. * Format Count
  64. *
  65. * @var int
  66. */
  67. private $_format = 0;
  68. /**
  69. * PHPExcel_Reader_IReadFilter instance
  70. *
  71. * @var PHPExcel_Reader_IReadFilter
  72. */
  73. private $_readFilter = null;
  74. /**
  75. * Create a new PHPExcel_Reader_SYLK
  76. */
  77. public function __construct() {
  78. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  79. }
  80. /**
  81. * Can the current PHPExcel_Reader_IReader read the file?
  82. *
  83. * @param string $pFileName
  84. * @return boolean
  85. */
  86. public function canRead($pFilename)
  87. {
  88. // Check if file exists
  89. if (!file_exists($pFilename)) {
  90. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  91. }
  92. // Read sample data (first 2 KB will do)
  93. $fh = fopen($pFilename, 'r');
  94. $data = fread($fh, 2048);
  95. fclose($fh);
  96. // Count delimiters in file
  97. $delimiterCount = substr_count($data, ';');
  98. if ($delimiterCount < 1) {
  99. return false;
  100. }
  101. // Analyze first line looking for ID; signature
  102. $lines = explode("\n", $data);
  103. if (substr($lines[0],0,4) != 'ID;P') {
  104. return false;
  105. }
  106. return true;
  107. }
  108. /**
  109. * Loads PHPExcel from file
  110. *
  111. * @param string $pFilename
  112. * @return PHPExcel
  113. * @throws Exception
  114. */
  115. public function load($pFilename)
  116. {
  117. // Create new PHPExcel
  118. $objPHPExcel = new PHPExcel();
  119. // Load into this instance
  120. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  121. }
  122. /**
  123. * Read filter
  124. *
  125. * @return PHPExcel_Reader_IReadFilter
  126. */
  127. public function getReadFilter() {
  128. return $this->_readFilter;
  129. }
  130. /**
  131. * Set read filter
  132. *
  133. * @param PHPExcel_Reader_IReadFilter $pValue
  134. */
  135. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  136. $this->_readFilter = $pValue;
  137. return $this;
  138. }
  139. /**
  140. * Set input encoding
  141. *
  142. * @param string $pValue Input encoding
  143. */
  144. public function setInputEncoding($pValue = 'ANSI')
  145. {
  146. $this->_inputEncoding = $pValue;
  147. return $this;
  148. }
  149. /**
  150. * Get input encoding
  151. *
  152. * @return string
  153. */
  154. public function getInputEncoding()
  155. {
  156. return $this->_inputEncoding;
  157. }
  158. /**
  159. * Loads PHPExcel from file into PHPExcel instance
  160. *
  161. * @param string $pFilename
  162. * @param PHPExcel $objPHPExcel
  163. * @return PHPExcel
  164. * @throws Exception
  165. */
  166. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  167. {
  168. // Check if file exists
  169. if (!file_exists($pFilename)) {
  170. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  171. }
  172. // Create new PHPExcel
  173. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
  174. $objPHPExcel->createSheet();
  175. }
  176. $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
  177. $fromFormats = array('\-', '\ ');
  178. $toFormats = array('-', ' ');
  179. // Open file
  180. $fileHandle = fopen($pFilename, 'r');
  181. if ($fileHandle === false) {
  182. throw new Exception("Could not open file $pFilename for reading.");
  183. }
  184. // Loop through file
  185. $rowData = array();
  186. $column = $row = '';
  187. // loop through one row (line) at a time in the file
  188. while (($rowData = fgets($fileHandle)) !== FALSE) {
  189. // convert SYLK encoded $rowData to UTF-8
  190. $rowData = PHPExcel_Shared_String::SYLKtoUTF8($rowData);
  191. // explode each row at semicolons while taking into account that literal semicolon (;)
  192. // is escaped like this (;;)
  193. $rowData = explode("\t",str_replace('¤',';',str_replace(';',"\t",str_replace(';;','¤',rtrim($rowData)))));
  194. $dataType = array_shift($rowData);
  195. // Read shared styles
  196. if ($dataType == 'P') {
  197. $formatArray = array();
  198. foreach($rowData as $rowDatum) {
  199. switch($rowDatum{0}) {
  200. case 'P' : $formatArray['numberformat']['code'] = str_replace($fromFormats,$toFormats,substr($rowDatum,1));
  201. break;
  202. case 'E' :
  203. case 'F' : $formatArray['font']['name'] = substr($rowDatum,1);
  204. break;
  205. case 'L' : $formatArray['font']['size'] = substr($rowDatum,1);
  206. break;
  207. case 'S' : $styleSettings = substr($rowDatum,1);
  208. for ($i=0;$i<strlen($styleSettings);++$i) {
  209. switch ($styleSettings{$i}) {
  210. case 'I' : $formatArray['font']['italic'] = true;
  211. break;
  212. case 'D' : $formatArray['font']['bold'] = true;
  213. break;
  214. case 'T' : $formatArray['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  215. break;
  216. case 'B' : $formatArray['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  217. break;
  218. case 'L' : $formatArray['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  219. break;
  220. case 'R' : $formatArray['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  221. break;
  222. }
  223. }
  224. break;
  225. }
  226. }
  227. $this->_formats['P'.$this->_format++] = $formatArray;
  228. // Read cell value data
  229. } elseif ($dataType == 'C') {
  230. $hasCalculatedValue = false;
  231. $cellData = $cellDataFormula = '';
  232. foreach($rowData as $rowDatum) {
  233. switch($rowDatum{0}) {
  234. case 'C' :
  235. case 'X' : $column = substr($rowDatum,1);
  236. break;
  237. case 'R' :
  238. case 'Y' : $row = substr($rowDatum,1);
  239. break;
  240. case 'K' : $cellData = substr($rowDatum,1);
  241. break;
  242. case 'E' : $cellDataFormula = '='.substr($rowDatum,1);
  243. // Convert R1C1 style references to A1 style references (but only when not quoted)
  244. $temp = explode('"',$cellDataFormula);
  245. $key = false;
  246. foreach($temp as &$value) {
  247. // Only count/replace in alternate array entries
  248. if ($key = !$key) {
  249. preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
  250. // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
  251. // through the formula from left to right. Reversing means that we work right to left.through
  252. // the formula
  253. $cellReferences = array_reverse($cellReferences);
  254. // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
  255. // then modify the formula to use that new reference
  256. foreach($cellReferences as $cellReference) {
  257. $rowReference = $cellReference[2][0];
  258. // Empty R reference is the current row
  259. if ($rowReference == '') $rowReference = $row;
  260. // Bracketed R references are relative to the current row
  261. if ($rowReference{0} == '[') $rowReference = $row + trim($rowReference,'[]');
  262. $columnReference = $cellReference[4][0];
  263. // Empty C reference is the current column
  264. if ($columnReference == '') $columnReference = $column;
  265. // Bracketed C references are relative to the current column
  266. if ($columnReference{0} == '[') $columnReference = $column + trim($columnReference,'[]');
  267. $A1CellReference = PHPExcel_Cell::stringFromColumnIndex($columnReference-1).$rowReference;
  268. $value = substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
  269. }
  270. }
  271. }
  272. unset($value);
  273. // Then rebuild the formula string
  274. $cellDataFormula = implode('"',$temp);
  275. $hasCalculatedValue = true;
  276. break;
  277. }
  278. }
  279. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
  280. $cellData = PHPExcel_Calculation::_unwrapResult($cellData);
  281. // Set cell value
  282. $objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
  283. if ($hasCalculatedValue) {
  284. $cellData = PHPExcel_Calculation::_unwrapResult($cellData);
  285. $objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setCalculatedValue($cellData);
  286. }
  287. // Read cell formatting
  288. } elseif ($dataType == 'F') {
  289. $formatStyle = $columnWidth = $styleSettings = '';
  290. $styleData = array();
  291. foreach($rowData as $rowDatum) {
  292. switch($rowDatum{0}) {
  293. case 'C' :
  294. case 'X' : $column = substr($rowDatum,1);
  295. break;
  296. case 'R' :
  297. case 'Y' : $row = substr($rowDatum,1);
  298. break;
  299. case 'P' : $formatStyle = $rowDatum;
  300. break;
  301. case 'W' : list($startCol,$endCol,$columnWidth) = explode(' ',substr($rowDatum,1));
  302. break;
  303. case 'S' : $styleSettings = substr($rowDatum,1);
  304. for ($i=0;$i<strlen($styleSettings);++$i) {
  305. switch ($styleSettings{$i}) {
  306. case 'I' : $styleData['font']['italic'] = true;
  307. break;
  308. case 'D' : $styleData['font']['bold'] = true;
  309. break;
  310. case 'T' : $styleData['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  311. break;
  312. case 'B' : $styleData['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  313. break;
  314. case 'L' : $styleData['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  315. break;
  316. case 'R' : $styleData['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  317. break;
  318. }
  319. }
  320. break;
  321. }
  322. }
  323. if (($formatStyle > '') && ($column > '') && ($row > '')) {
  324. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
  325. $objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($this->_formats[$formatStyle]);
  326. }
  327. if ((count($styleData) > 0) && ($column > '') && ($row > '')) {
  328. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
  329. $objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($styleData);
  330. }
  331. if ($columnWidth > '') {
  332. if ($startCol == $endCol) {
  333. $startCol = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
  334. $objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
  335. } else {
  336. $startCol = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
  337. $endCol = PHPExcel_Cell::stringFromColumnIndex($endCol-1);
  338. $objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
  339. do {
  340. $objPHPExcel->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth);
  341. } while ($startCol != $endCol);
  342. }
  343. }
  344. } else {
  345. foreach($rowData as $rowDatum) {
  346. switch($rowDatum{0}) {
  347. case 'C' :
  348. case 'X' : $column = substr($rowDatum,1);
  349. break;
  350. case 'R' :
  351. case 'Y' : $row = substr($rowDatum,1);
  352. break;
  353. }
  354. }
  355. }
  356. }
  357. // Close file
  358. fclose($fileHandle);
  359. // Return
  360. return $objPHPExcel;
  361. }
  362. /**
  363. * Get sheet index
  364. *
  365. * @return int
  366. */
  367. public function getSheetIndex() {
  368. return $this->_sheetIndex;
  369. }
  370. /**
  371. * Set sheet index
  372. *
  373. * @param int $pValue Sheet index
  374. * @return PHPExcel_Reader_SYLK
  375. */
  376. public function setSheetIndex($pValue = 0) {
  377. $this->_sheetIndex = $pValue;
  378. return $this;
  379. }
  380. }