PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/output/plugins/PHPExcel/Reader/SYLK.php

https://gitlab.com/Lidbary/PHPRunner
PHP | 414 lines | 231 code | 35 blank | 148 comment | 44 complexity | 5f9385a2d75beccf2a968cb5711455f4 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.5, 2010-12-10
  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 - 2010 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. foreach($temp as $key => &$value) {
  246. // Only count/replace in alternate array entries
  247. if (($key % 2) == 0) {
  248. preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
  249. // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
  250. // through the formula from left to right. Reversing means that we work right to left.through
  251. // the formula
  252. $cellReferences = array_reverse($cellReferences);
  253. // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent,
  254. // then modify the formula to use that new reference
  255. foreach($cellReferences as $cellReference) {
  256. $rowReference = $cellReference[2][0];
  257. // Empty R reference is the current row
  258. if ($rowReference == '') $rowReference = $row;
  259. // Bracketed R references are relative to the current row
  260. if ($rowReference{0} == '[') $rowReference = $row + trim($rowReference,'[]');
  261. $columnReference = $cellReference[4][0];
  262. // Empty C reference is the current column
  263. if ($columnReference == '') $columnReference = $column;
  264. // Bracketed C references are relative to the current column
  265. if ($columnReference{0} == '[') $columnReference = $column + trim($columnReference,'[]');
  266. $A1CellReference = PHPExcel_Cell::stringFromColumnIndex($columnReference-1).$rowReference;
  267. $value = substr_replace($value,$A1CellReference,$cellReference[0][1],strlen($cellReference[0][0]));
  268. }
  269. }
  270. }
  271. unset($value);
  272. // Then rebuild the formula string
  273. $cellDataFormula = implode('"',$temp);
  274. $hasCalculatedValue = true;
  275. break;
  276. }
  277. }
  278. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
  279. $cellData = PHPExcel_Calculation::_unwrapResult($cellData);
  280. // Set cell value
  281. $objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData);
  282. if ($hasCalculatedValue) {
  283. $cellData = PHPExcel_Calculation::_unwrapResult($cellData);
  284. $objPHPExcel->getActiveSheet()->getCell($columnLetter.$row)->setCalculatedValue($cellData);
  285. }
  286. // Read cell formatting
  287. } elseif ($dataType == 'F') {
  288. $formatStyle = $columnWidth = $styleSettings = '';
  289. $styleData = array();
  290. foreach($rowData as $rowDatum) {
  291. switch($rowDatum{0}) {
  292. case 'C' :
  293. case 'X' : $column = substr($rowDatum,1);
  294. break;
  295. case 'R' :
  296. case 'Y' : $row = substr($rowDatum,1);
  297. break;
  298. case 'P' : $formatStyle = $rowDatum;
  299. break;
  300. case 'W' : list($startCol,$endCol,$columnWidth) = explode(' ',substr($rowDatum,1));
  301. break;
  302. case 'S' : $styleSettings = substr($rowDatum,1);
  303. for ($i=0;$i<strlen($styleSettings);++$i) {
  304. switch ($styleSettings{$i}) {
  305. case 'I' : $styleData['font']['italic'] = true;
  306. break;
  307. case 'D' : $styleData['font']['bold'] = true;
  308. break;
  309. case 'T' : $styleData['borders']['top']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  310. break;
  311. case 'B' : $styleData['borders']['bottom']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  312. break;
  313. case 'L' : $styleData['borders']['left']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  314. break;
  315. case 'R' : $styleData['borders']['right']['style'] = PHPExcel_Style_Border::BORDER_THIN;
  316. break;
  317. }
  318. }
  319. break;
  320. }
  321. }
  322. if (($formatStyle > '') && ($column > '') && ($row > '')) {
  323. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
  324. $objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($this->_formats[$formatStyle]);
  325. }
  326. if ((count($styleData) > 0) && ($column > '') && ($row > '')) {
  327. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($column-1);
  328. $objPHPExcel->getActiveSheet()->getStyle($columnLetter.$row)->applyFromArray($styleData);
  329. }
  330. if ($columnWidth > '') {
  331. if ($startCol == $endCol) {
  332. $startCol = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
  333. $objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
  334. } else {
  335. $startCol = PHPExcel_Cell::stringFromColumnIndex($startCol-1);
  336. $endCol = PHPExcel_Cell::stringFromColumnIndex($endCol-1);
  337. $objPHPExcel->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth);
  338. do {
  339. $objPHPExcel->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth);
  340. } while ($startCol != $endCol);
  341. }
  342. }
  343. } else {
  344. foreach($rowData as $rowDatum) {
  345. switch($rowDatum{0}) {
  346. case 'C' :
  347. case 'X' : $column = substr($rowDatum,1);
  348. break;
  349. case 'R' :
  350. case 'Y' : $row = substr($rowDatum,1);
  351. break;
  352. }
  353. }
  354. }
  355. }
  356. // Close file
  357. fclose($fileHandle);
  358. // Return
  359. return $objPHPExcel;
  360. }
  361. /**
  362. * Get sheet index
  363. *
  364. * @return int
  365. */
  366. public function getSheetIndex() {
  367. return $this->_sheetIndex;
  368. }
  369. /**
  370. * Set sheet index
  371. *
  372. * @param int $pValue Sheet index
  373. * @return PHPExcel_Reader_SYLK
  374. */
  375. public function setSheetIndex($pValue = 0) {
  376. $this->_sheetIndex = $pValue;
  377. return $this;
  378. }
  379. }