PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/add-ons/PHPExcel/PHPExcel/Reader/CSV.php

https://github.com/jcplat/console-seolan
PHP | 362 lines | 145 code | 44 blank | 173 comment | 23 complexity | 6a07f57b88bd0580b4800dbfac8133c0 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.1, 2009-11-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  33. }
  34. /** PHPExcel */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel.php';
  36. /** PHPExcel_Reader_IReader */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/IReader.php';
  38. /** PHPExcel_Worksheet */
  39. require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
  40. /** PHPExcel_Cell */
  41. require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
  42. /** PHPExcel_Reader_DefaultReadFilter */
  43. require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/DefaultReadFilter.php';
  44. /**
  45. * PHPExcel_Reader_CSV
  46. *
  47. * @category PHPExcel
  48. * @package PHPExcel_Reader
  49. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  50. */
  51. class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
  52. {
  53. /**
  54. * Input encoding
  55. *
  56. * @var string
  57. */
  58. private $_inputEncoding;
  59. /**
  60. * Delimiter
  61. *
  62. * @var string
  63. */
  64. private $_delimiter;
  65. /**
  66. * Enclosure
  67. *
  68. * @var string
  69. */
  70. private $_enclosure;
  71. /**
  72. * Line ending
  73. *
  74. * @var string
  75. */
  76. private $_lineEnding;
  77. /**
  78. * Sheet index to read
  79. *
  80. * @var int
  81. */
  82. private $_sheetIndex;
  83. /**
  84. * PHPExcel_Reader_IReadFilter instance
  85. *
  86. * @var PHPExcel_Reader_IReadFilter
  87. */
  88. private $_readFilter = null;
  89. /**
  90. * Create a new PHPExcel_Reader_CSV
  91. */
  92. public function __construct() {
  93. $this->_inputEncoding = 'UTF-8';
  94. $this->_delimiter = ',';
  95. $this->_enclosure = '"';
  96. $this->_lineEnding = PHP_EOL;
  97. $this->_sheetIndex = 0;
  98. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  99. }
  100. /**
  101. * Can the current PHPExcel_Reader_IReader read the file?
  102. *
  103. * @param string $pFileName
  104. * @return boolean
  105. */
  106. public function canRead($pFilename)
  107. {
  108. // Check if file exists
  109. if (!file_exists($pFilename)) {
  110. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  111. }
  112. // Read sample data (first 2 KB will do)
  113. $fh = fopen($pFilename, 'r');
  114. $data = fread($fh, 2048);
  115. fclose($fh);
  116. // Count delimiters in file
  117. $delimiterCount = array(
  118. ',' => substr_count($data, ','),
  119. ';' => substr_count($data, ';'),
  120. "\t" => substr_count($data, "\t"),
  121. '|' => substr_count($data, '|')
  122. );
  123. arsort($delimiterCount, SORT_NUMERIC);
  124. $probableDelimiter = array_shift(array_flip($delimiterCount));
  125. // Analyze first few lines and check if field count is equal
  126. $lines = explode("\n", $data);
  127. $fieldCount = null;
  128. for ($i = 0; $i < 5 && $i < count($lines); $i++) {
  129. if (is_null($fieldCount))
  130. $fieldCount = substr_count($lines[$i], $probableDelimiter);
  131. if ($fieldCount != substr_count($lines[$i], $probableDelimiter))
  132. return false;
  133. }
  134. return true;
  135. }
  136. /**
  137. * Loads PHPExcel from file
  138. *
  139. * @param string $pFilename
  140. * @throws Exception
  141. */
  142. public function load($pFilename)
  143. {
  144. // Create new PHPExcel
  145. $objPHPExcel = new PHPExcel();
  146. // Load into this instance
  147. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  148. }
  149. /**
  150. * Read filter
  151. *
  152. * @return PHPExcel_Reader_IReadFilter
  153. */
  154. public function getReadFilter() {
  155. return $this->_readFilter;
  156. }
  157. /**
  158. * Set read filter
  159. *
  160. * @param PHPExcel_Reader_IReadFilter $pValue
  161. */
  162. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  163. $this->_readFilter = $pValue;
  164. }
  165. /**
  166. * Set input encoding
  167. *
  168. * @param string $pValue Input encoding
  169. */
  170. public function setInputEncoding($pValue = 'UTF-8')
  171. {
  172. $this->_inputEncoding = $pValue;
  173. }
  174. /**
  175. * Get input encoding
  176. *
  177. * @return string
  178. */
  179. public function getInputEncoding()
  180. {
  181. return $this->_inputEncoding;
  182. }
  183. /**
  184. * Loads PHPExcel from file into PHPExcel instance
  185. *
  186. * @param string $pFilename
  187. * @param PHPExcel $objPHPExcel
  188. * @throws Exception
  189. */
  190. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  191. {
  192. // Check if file exists
  193. if (!file_exists($pFilename)) {
  194. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  195. }
  196. // Create new PHPExcel
  197. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
  198. $objPHPExcel->createSheet();
  199. }
  200. $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
  201. // Open file
  202. $fileHandle = fopen($pFilename, 'r');
  203. if ($fileHandle === false) {
  204. throw new Exception("Could not open file $pFilename for reading.");
  205. }
  206. // Skip BOM, if any
  207. switch ($this->_inputEncoding) {
  208. case 'UTF-8':
  209. fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
  210. fseek($fileHandle, 3) : fseek($fileHandle, 0);
  211. break;
  212. default:
  213. break;
  214. }
  215. // Loop through file
  216. $currentRow = 0;
  217. $rowData = array();
  218. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  219. ++$currentRow;
  220. $rowDataCount = count($rowData);
  221. for ($i = 0; $i < $rowDataCount; ++$i) {
  222. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i);
  223. if ($rowData[$i] != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
  224. // Unescape enclosures
  225. $rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  226. $rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  227. // Convert encoding if necessary
  228. if ($this->_inputEncoding !== 'UTF-8') {
  229. $rowData[$i] = PHPExcel_Shared_String::ConvertEncoding($rowData[$i], 'UTF-8', $this->_inputEncoding);
  230. }
  231. // Set cell value
  232. $objPHPExcel->getActiveSheet()->setCellValue(
  233. $columnLetter . $currentRow, $rowData[$i]
  234. );
  235. }
  236. }
  237. }
  238. // Close file
  239. fclose($fileHandle);
  240. // Return
  241. return $objPHPExcel;
  242. }
  243. /**
  244. * Get delimiter
  245. *
  246. * @return string
  247. */
  248. public function getDelimiter() {
  249. return $this->_delimiter;
  250. }
  251. /**
  252. * Set delimiter
  253. *
  254. * @param string $pValue Delimiter, defaults to ,
  255. * @return PHPExcel_Reader_CSV
  256. */
  257. public function setDelimiter($pValue = ',') {
  258. $this->_delimiter = $pValue;
  259. return $this;
  260. }
  261. /**
  262. * Get enclosure
  263. *
  264. * @return string
  265. */
  266. public function getEnclosure() {
  267. return $this->_enclosure;
  268. }
  269. /**
  270. * Set enclosure
  271. *
  272. * @param string $pValue Enclosure, defaults to "
  273. * @return PHPExcel_Reader_CSV
  274. */
  275. public function setEnclosure($pValue = '"') {
  276. if ($pValue == '') {
  277. $pValue = '"';
  278. }
  279. $this->_enclosure = $pValue;
  280. return $this;
  281. }
  282. /**
  283. * Get line ending
  284. *
  285. * @return string
  286. */
  287. public function getLineEnding() {
  288. return $this->_lineEnding;
  289. }
  290. /**
  291. * Set line ending
  292. *
  293. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  294. * @return PHPExcel_Reader_CSV
  295. */
  296. public function setLineEnding($pValue = PHP_EOL) {
  297. $this->_lineEnding = $pValue;
  298. return $this;
  299. }
  300. /**
  301. * Get sheet index
  302. *
  303. * @return int
  304. */
  305. public function getSheetIndex() {
  306. return $this->_sheetIndex;
  307. }
  308. /**
  309. * Set sheet index
  310. *
  311. * @param int $pValue Sheet index
  312. * @return PHPExcel_Reader_CSV
  313. */
  314. public function setSheetIndex($pValue = 0) {
  315. $this->_sheetIndex = $pValue;
  316. return $this;
  317. }
  318. }