/protected/libs/phpexcel/Classes/PHPExcel/Reader/CSV.php

https://github.com/allinside/Yii-CMS · PHP · 411 lines · 153 code · 44 blank · 214 comment · 25 complexity · 1a869847a13689676980f8c1bf5d5e88 MD5 · raw file

  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_CSV
  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_CSV 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. * PHPExcel_Reader_IReadFilter instance
  95. *
  96. * @access private
  97. * @var PHPExcel_Reader_IReadFilter
  98. */
  99. private $_readFilter = null;
  100. /**
  101. * Create a new PHPExcel_Reader_CSV
  102. */
  103. public function __construct() {
  104. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  105. } // function __construct()
  106. /**
  107. * Can the current PHPExcel_Reader_IReader read the file?
  108. *
  109. * @access public
  110. * @param string $pFileName
  111. * @return boolean
  112. * @throws Exception
  113. */
  114. public function canRead($pFilename)
  115. {
  116. // Check if file exists
  117. if (!file_exists($pFilename)) {
  118. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  119. }
  120. return true;
  121. } // function canRead()
  122. /**
  123. * Loads PHPExcel from file
  124. *
  125. * @access public
  126. * @param string $pFilename
  127. * @return PHPExcel
  128. * @throws Exception
  129. */
  130. public function load($pFilename)
  131. {
  132. // Create new PHPExcel
  133. $objPHPExcel = new PHPExcel();
  134. // Load into this instance
  135. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  136. } // function load()
  137. /**
  138. * Read filter
  139. *
  140. * @access public
  141. * @return PHPExcel_Reader_IReadFilter
  142. */
  143. public function getReadFilter() {
  144. return $this->_readFilter;
  145. } // function getReadFilter()
  146. /**
  147. * Set read filter
  148. *
  149. * @access public
  150. * @param PHPExcel_Reader_IReadFilter $pValue
  151. */
  152. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  153. $this->_readFilter = $pValue;
  154. return $this;
  155. } // function setReadFilter()
  156. /**
  157. * Set input encoding
  158. *
  159. * @access public
  160. * @param string $pValue Input encoding
  161. */
  162. public function setInputEncoding($pValue = 'UTF-8')
  163. {
  164. $this->_inputEncoding = $pValue;
  165. return $this;
  166. } // function setInputEncoding()
  167. /**
  168. * Get input encoding
  169. *
  170. * @access public
  171. * @return string
  172. */
  173. public function getInputEncoding()
  174. {
  175. return $this->_inputEncoding;
  176. } // function getInputEncoding()
  177. /**
  178. * Loads PHPExcel from file into PHPExcel instance
  179. *
  180. * @access public
  181. * @param string $pFilename
  182. * @param PHPExcel $objPHPExcel
  183. * @return PHPExcel
  184. * @throws Exception
  185. */
  186. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  187. {
  188. // Check if file exists
  189. if (!file_exists($pFilename)) {
  190. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  191. }
  192. // Create new PHPExcel
  193. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
  194. $objPHPExcel->createSheet();
  195. }
  196. $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
  197. // Open file
  198. $fileHandle = fopen($pFilename, 'r');
  199. if ($fileHandle === false) {
  200. throw new Exception("Could not open file $pFilename for reading.");
  201. }
  202. // Skip BOM, if any
  203. switch ($this->_inputEncoding) {
  204. case 'UTF-8':
  205. fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
  206. fseek($fileHandle, 3) : fseek($fileHandle, 0);
  207. break;
  208. case 'UTF-16LE':
  209. fgets($fileHandle, 3) == "\xFF\xFE" ?
  210. fseek($fileHandle, 2) : fseek($fileHandle, 0);
  211. break;
  212. case 'UTF-16BE':
  213. fgets($fileHandle, 3) == "\xFE\xFF" ?
  214. fseek($fileHandle, 2) : fseek($fileHandle, 0);
  215. break;
  216. case 'UTF-32LE':
  217. fgets($fileHandle, 5) == "\xFF\xFE\x00\x00" ?
  218. fseek($fileHandle, 4) : fseek($fileHandle, 0);
  219. break;
  220. case 'UTF-32BE':
  221. fgets($fileHandle, 5) == "\x00\x00\xFE\xFF" ?
  222. fseek($fileHandle, 4) : fseek($fileHandle, 0);
  223. break;
  224. default:
  225. break;
  226. }
  227. $escapeEnclosures = array( "\\" . $this->_enclosure,
  228. $this->_enclosure . $this->_enclosure
  229. );
  230. // Set our starting row based on whether we're in contiguous mode or not
  231. $currentRow = 1;
  232. if ($this->_contiguous) {
  233. $currentRow = ($this->_contiguousRow == -1) ? $objPHPExcel->getActiveSheet()->getHighestRow(): $this->_contiguousRow;
  234. }
  235. // Loop through each line of the file in turn
  236. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  237. $columnLetter = 'A';
  238. foreach($rowData as $rowDatum) {
  239. if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
  240. // Unescape enclosures
  241. $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
  242. // Convert encoding if necessary
  243. if ($this->_inputEncoding !== 'UTF-8') {
  244. $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
  245. }
  246. // Set cell value
  247. $objPHPExcel->getActiveSheet()->getCell($columnLetter . $currentRow)->setValue($rowDatum);
  248. }
  249. ++$columnLetter;
  250. }
  251. ++$currentRow;
  252. }
  253. // Close file
  254. fclose($fileHandle);
  255. if ($this->_contiguous) {
  256. $this->_contiguousRow = $currentRow;
  257. }
  258. // Return
  259. return $objPHPExcel;
  260. } // function loadIntoExisting()
  261. /**
  262. * Get delimiter
  263. *
  264. * @access public
  265. * @return string
  266. */
  267. public function getDelimiter() {
  268. return $this->_delimiter;
  269. } // function getDelimiter()
  270. /**
  271. * Set delimiter
  272. *
  273. * @access public
  274. * @param string $pValue Delimiter, defaults to ,
  275. * @return PHPExcel_Reader_CSV
  276. */
  277. public function setDelimiter($pValue = ',') {
  278. $this->_delimiter = $pValue;
  279. return $this;
  280. } // function setDelimiter()
  281. /**
  282. * Get enclosure
  283. *
  284. * @access public
  285. * @return string
  286. */
  287. public function getEnclosure() {
  288. return $this->_enclosure;
  289. } // function getEnclosure()
  290. /**
  291. * Set enclosure
  292. *
  293. * @access public
  294. * @param string $pValue Enclosure, defaults to "
  295. * @return PHPExcel_Reader_CSV
  296. */
  297. public function setEnclosure($pValue = '"') {
  298. if ($pValue == '') {
  299. $pValue = '"';
  300. }
  301. $this->_enclosure = $pValue;
  302. return $this;
  303. } // function setEnclosure()
  304. /**
  305. * Get line ending
  306. *
  307. * @access public
  308. * @return string
  309. */
  310. public function getLineEnding() {
  311. return $this->_lineEnding;
  312. } // function getLineEnding()
  313. /**
  314. * Set line ending
  315. *
  316. * @access public
  317. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  318. * @return PHPExcel_Reader_CSV
  319. */
  320. public function setLineEnding($pValue = PHP_EOL) {
  321. $this->_lineEnding = $pValue;
  322. return $this;
  323. } // function setLineEnding()
  324. /**
  325. * Get sheet index
  326. *
  327. * @access public
  328. * @return int
  329. */
  330. public function getSheetIndex() {
  331. return $this->_sheetIndex;
  332. } // function getSheetIndex()
  333. /**
  334. * Set sheet index
  335. *
  336. * @access public
  337. * @param int $pValue Sheet index
  338. * @return PHPExcel_Reader_CSV
  339. */
  340. public function setSheetIndex($pValue = 0) {
  341. $this->_sheetIndex = $pValue;
  342. return $this;
  343. } // function setSheetIndex()
  344. /**
  345. * Set Contiguous
  346. *
  347. * @access public
  348. * @param string $pValue Input encoding
  349. */
  350. public function setContiguous($contiguous = false)
  351. {
  352. $this->_contiguous = (bool)$contiguous;
  353. if (!$contiguous) {
  354. $this->_contiguousRow = -1;
  355. }
  356. return $this;
  357. } // function setInputEncoding()
  358. /**
  359. * Get Contiguous
  360. *
  361. * @access public
  362. * @return boolean
  363. */
  364. public function getContiguous() {
  365. return $this->_contiguous;
  366. } // function getSheetIndex()
  367. }