PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/PHPExcel/Reader/CSV.php

https://github.com/iGroup/PHPExcel
PHP | 473 lines | 197 code | 68 blank | 208 comment | 36 complexity | 4bb3f5be6721ad99358728779cd08da2 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 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_CSV
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Reader
  40. * @copyright Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  43. {
  44. /**
  45. * Input encoding
  46. *
  47. * @access private
  48. * @var string
  49. */
  50. private $_inputEncoding = 'UTF-8';
  51. /**
  52. * Delimiter
  53. *
  54. * @access private
  55. * @var string
  56. */
  57. private $_delimiter = ',';
  58. /**
  59. * Enclosure
  60. *
  61. * @access private
  62. * @var string
  63. */
  64. private $_enclosure = '"';
  65. /**
  66. * Line ending
  67. *
  68. * @access private
  69. * @var string
  70. */
  71. private $_lineEnding = PHP_EOL;
  72. /**
  73. * Sheet index to read
  74. *
  75. * @access private
  76. * @var int
  77. */
  78. private $_sheetIndex = 0;
  79. /**
  80. * Load rows contiguously
  81. *
  82. * @access private
  83. * @var int
  84. */
  85. private $_contiguous = false;
  86. /**
  87. * Row counter for loading rows contiguously
  88. *
  89. * @access private
  90. * @var int
  91. */
  92. private $_contiguousRow = -1;
  93. /**
  94. * Create a new PHPExcel_Reader_CSV
  95. */
  96. public function __construct() {
  97. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  98. } // function __construct()
  99. /**
  100. * Can the current PHPExcel_Reader_IReader read the file?
  101. *
  102. * @access public
  103. * @param string $pFileName
  104. * @return boolean
  105. * @throws PHPExcel_Reader_Exception
  106. */
  107. public function canRead($pFilename)
  108. {
  109. // Check if file exists
  110. if (!file_exists($pFilename)) {
  111. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  112. }
  113. return true;
  114. } // function canRead()
  115. /**
  116. * Set input encoding
  117. *
  118. * @access public
  119. * @param string $pValue Input encoding
  120. */
  121. public function setInputEncoding($pValue = 'UTF-8')
  122. {
  123. $this->_inputEncoding = $pValue;
  124. return $this;
  125. } // function setInputEncoding()
  126. /**
  127. * Get input encoding
  128. *
  129. * @access public
  130. * @return string
  131. */
  132. public function getInputEncoding()
  133. {
  134. return $this->_inputEncoding;
  135. } // function getInputEncoding()
  136. /**
  137. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  138. *
  139. * @access public
  140. * @param string $pFilename
  141. * @throws PHPExcel_Reader_Exception
  142. */
  143. public function listWorksheetInfo($pFilename)
  144. {
  145. // Check if file exists
  146. if (!file_exists($pFilename)) {
  147. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  148. }
  149. // Open file
  150. $fileHandle = fopen($pFilename, 'r');
  151. if ($fileHandle === false) {
  152. throw new PHPExcel_Reader_Exception("Could not open file " . $pFilename . " for reading.");
  153. }
  154. // Skip BOM, if any
  155. switch ($this->_inputEncoding) {
  156. case 'UTF-8':
  157. fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
  158. fseek($fileHandle, 3) : fseek($fileHandle, 0);
  159. break;
  160. case 'UTF-16LE':
  161. fgets($fileHandle, 3) == "\xFF\xFE" ?
  162. fseek($fileHandle, 2) : fseek($fileHandle, 0);
  163. break;
  164. case 'UTF-16BE':
  165. fgets($fileHandle, 3) == "\xFE\xFF" ?
  166. fseek($fileHandle, 2) : fseek($fileHandle, 0);
  167. break;
  168. case 'UTF-32LE':
  169. fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
  170. fseek($fileHandle, 4) : fseek($fileHandle, 0);
  171. break;
  172. case 'UTF-32BE':
  173. fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
  174. fseek($fileHandle, 4) : fseek($fileHandle, 0);
  175. break;
  176. default:
  177. break;
  178. }
  179. $escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure );
  180. $worksheetInfo = array();
  181. $worksheetInfo[0]['worksheetName'] = 'Worksheet';
  182. $worksheetInfo[0]['lastColumnLetter'] = 'A';
  183. $worksheetInfo[0]['lastColumnIndex'] = 0;
  184. $worksheetInfo[0]['totalRows'] = 0;
  185. $worksheetInfo[0]['totalColumns'] = 0;
  186. // Loop through each line of the file in turn
  187. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  188. $worksheetInfo[0]['totalRows']++;
  189. $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
  190. }
  191. $worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
  192. $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
  193. // Close file
  194. fclose($fileHandle);
  195. return $worksheetInfo;
  196. }
  197. /**
  198. * Loads PHPExcel from file
  199. *
  200. * @access public
  201. * @param string $pFilename
  202. * @return PHPExcel
  203. * @throws PHPExcel_Reader_Exception
  204. */
  205. public function load($pFilename)
  206. {
  207. // Create new PHPExcel
  208. $objPHPExcel = new PHPExcel();
  209. // Load into this instance
  210. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  211. } // function load()
  212. /**
  213. * Loads PHPExcel from file into PHPExcel instance
  214. *
  215. * @access public
  216. * @param string $pFilename
  217. * @param PHPExcel $objPHPExcel
  218. * @return PHPExcel
  219. * @throws PHPExcel_Reader_Exception
  220. */
  221. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  222. {
  223. // Check if file exists
  224. if (!file_exists($pFilename)) {
  225. throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  226. }
  227. // Create new PHPExcel
  228. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
  229. $objPHPExcel->createSheet();
  230. }
  231. $sheet = $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
  232. $lineEnding = ini_get('auto_detect_line_endings');
  233. ini_set('auto_detect_line_endings', true);
  234. // Open file
  235. $fileHandle = fopen($pFilename, 'r');
  236. if ($fileHandle === false) {
  237. throw new PHPExcel_Reader_Exception("Could not open file $pFilename for reading.");
  238. }
  239. // Skip BOM, if any
  240. switch ($this->_inputEncoding) {
  241. case 'UTF-8':
  242. fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
  243. fseek($fileHandle, 3) : fseek($fileHandle, 0);
  244. break;
  245. case 'UTF-16LE':
  246. fgets($fileHandle, 3) == "\xFF\xFE" ?
  247. fseek($fileHandle, 2) : fseek($fileHandle, 0);
  248. break;
  249. case 'UTF-16BE':
  250. fgets($fileHandle, 3) == "\xFE\xFF" ?
  251. fseek($fileHandle, 2) : fseek($fileHandle, 0);
  252. break;
  253. case 'UTF-32LE':
  254. fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
  255. fseek($fileHandle, 4) : fseek($fileHandle, 0);
  256. break;
  257. case 'UTF-32BE':
  258. fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
  259. fseek($fileHandle, 4) : fseek($fileHandle, 0);
  260. break;
  261. default:
  262. break;
  263. }
  264. $escapeEnclosures = array( "\\" . $this->_enclosure,
  265. $this->_enclosure . $this->_enclosure
  266. );
  267. // Set our starting row based on whether we're in contiguous mode or not
  268. $currentRow = 1;
  269. if ($this->_contiguous) {
  270. $currentRow = ($this->_contiguousRow == -1) ? $sheet->getHighestRow(): $this->_contiguousRow;
  271. }
  272. // Loop through each line of the file in turn
  273. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  274. $columnLetter = 'A';
  275. foreach($rowData as $rowDatum) {
  276. if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
  277. // Unescape enclosures
  278. $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
  279. // Convert encoding if necessary
  280. if ($this->_inputEncoding !== 'UTF-8') {
  281. $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
  282. }
  283. // Set cell value
  284. $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
  285. }
  286. ++$columnLetter;
  287. }
  288. ++$currentRow;
  289. }
  290. // Close file
  291. fclose($fileHandle);
  292. if ($this->_contiguous) {
  293. $this->_contiguousRow = $currentRow;
  294. }
  295. ini_set('auto_detect_line_endings', $lineEnding);
  296. // Return
  297. return $objPHPExcel;
  298. } // function loadIntoExisting()
  299. /**
  300. * Get delimiter
  301. *
  302. * @access public
  303. * @return string
  304. */
  305. public function getDelimiter() {
  306. return $this->_delimiter;
  307. } // function getDelimiter()
  308. /**
  309. * Set delimiter
  310. *
  311. * @access public
  312. * @param string $pValue Delimiter, defaults to ,
  313. * @return PHPExcel_Reader_CSV
  314. */
  315. public function setDelimiter($pValue = ',') {
  316. $this->_delimiter = $pValue;
  317. return $this;
  318. } // function setDelimiter()
  319. /**
  320. * Get enclosure
  321. *
  322. * @access public
  323. * @return string
  324. */
  325. public function getEnclosure() {
  326. return $this->_enclosure;
  327. } // function getEnclosure()
  328. /**
  329. * Set enclosure
  330. *
  331. * @access public
  332. * @param string $pValue Enclosure, defaults to "
  333. * @return PHPExcel_Reader_CSV
  334. */
  335. public function setEnclosure($pValue = '"') {
  336. if ($pValue == '') {
  337. $pValue = '"';
  338. }
  339. $this->_enclosure = $pValue;
  340. return $this;
  341. } // function setEnclosure()
  342. /**
  343. * Get line ending
  344. *
  345. * @access public
  346. * @return string
  347. */
  348. public function getLineEnding() {
  349. return $this->_lineEnding;
  350. } // function getLineEnding()
  351. /**
  352. * Set line ending
  353. *
  354. * @access public
  355. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  356. * @return PHPExcel_Reader_CSV
  357. */
  358. public function setLineEnding($pValue = PHP_EOL) {
  359. $this->_lineEnding = $pValue;
  360. return $this;
  361. } // function setLineEnding()
  362. /**
  363. * Get sheet index
  364. *
  365. * @access public
  366. * @return int
  367. */
  368. public function getSheetIndex() {
  369. return $this->_sheetIndex;
  370. } // function getSheetIndex()
  371. /**
  372. * Set sheet index
  373. *
  374. * @access public
  375. * @param int $pValue Sheet index
  376. * @return PHPExcel_Reader_CSV
  377. */
  378. public function setSheetIndex($pValue = 0) {
  379. $this->_sheetIndex = $pValue;
  380. return $this;
  381. } // function setSheetIndex()
  382. /**
  383. * Set Contiguous
  384. *
  385. * @access public
  386. * @param string $pValue Input encoding
  387. */
  388. public function setContiguous($contiguous = false)
  389. {
  390. $this->_contiguous = (bool)$contiguous;
  391. if (!$contiguous) {
  392. $this->_contiguousRow = -1;
  393. }
  394. return $this;
  395. } // function setInputEncoding()
  396. /**
  397. * Get Contiguous
  398. *
  399. * @access public
  400. * @return boolean
  401. */
  402. public function getContiguous() {
  403. return $this->_contiguous;
  404. } // function getSheetIndex()
  405. }