/branches/v1.7.2/Classes/PHPExcel/Reader/CSV.php

# · PHP · 337 lines · 126 code · 41 blank · 170 comment · 18 complexity · d7219f44fbf5f11fb7ab4124ac1cd327 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 ##VERSION##, ##DATE##
  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 - 2010 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. return true;
  113. }
  114. /**
  115. * Loads PHPExcel from file
  116. *
  117. * @param string $pFilename
  118. * @throws Exception
  119. */
  120. public function load($pFilename)
  121. {
  122. // Create new PHPExcel
  123. $objPHPExcel = new PHPExcel();
  124. // Load into this instance
  125. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  126. }
  127. /**
  128. * Read filter
  129. *
  130. * @return PHPExcel_Reader_IReadFilter
  131. */
  132. public function getReadFilter() {
  133. return $this->_readFilter;
  134. }
  135. /**
  136. * Set read filter
  137. *
  138. * @param PHPExcel_Reader_IReadFilter $pValue
  139. */
  140. public function setReadFilter(PHPExcel_Reader_IReadFilter $pValue) {
  141. $this->_readFilter = $pValue;
  142. }
  143. /**
  144. * Set input encoding
  145. *
  146. * @param string $pValue Input encoding
  147. */
  148. public function setInputEncoding($pValue = 'UTF-8')
  149. {
  150. $this->_inputEncoding = $pValue;
  151. }
  152. /**
  153. * Get input encoding
  154. *
  155. * @return string
  156. */
  157. public function getInputEncoding()
  158. {
  159. return $this->_inputEncoding;
  160. }
  161. /**
  162. * Loads PHPExcel from file into PHPExcel instance
  163. *
  164. * @param string $pFilename
  165. * @param PHPExcel $objPHPExcel
  166. * @throws Exception
  167. */
  168. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  169. {
  170. // Check if file exists
  171. if (!file_exists($pFilename)) {
  172. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  173. }
  174. // Create new PHPExcel
  175. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
  176. $objPHPExcel->createSheet();
  177. }
  178. $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
  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. // Skip BOM, if any
  185. switch ($this->_inputEncoding) {
  186. case 'UTF-8':
  187. fgets($fileHandle, 4) == "\xEF\xBB\xBF" ?
  188. fseek($fileHandle, 3) : fseek($fileHandle, 0);
  189. break;
  190. default:
  191. break;
  192. }
  193. // Loop through file
  194. $currentRow = 0;
  195. $rowData = array();
  196. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  197. ++$currentRow;
  198. $rowDataCount = count($rowData);
  199. for ($i = 0; $i < $rowDataCount; ++$i) {
  200. $columnLetter = PHPExcel_Cell::stringFromColumnIndex($i);
  201. if ($rowData[$i] != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
  202. // Unescape enclosures
  203. $rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  204. $rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  205. // Convert encoding if necessary
  206. if ($this->_inputEncoding !== 'UTF-8') {
  207. $rowData[$i] = PHPExcel_Shared_String::ConvertEncoding($rowData[$i], 'UTF-8', $this->_inputEncoding);
  208. }
  209. // Set cell value
  210. $objPHPExcel->getActiveSheet()->setCellValue(
  211. $columnLetter . $currentRow, $rowData[$i]
  212. );
  213. }
  214. }
  215. }
  216. // Close file
  217. fclose($fileHandle);
  218. // Return
  219. return $objPHPExcel;
  220. }
  221. /**
  222. * Get delimiter
  223. *
  224. * @return string
  225. */
  226. public function getDelimiter() {
  227. return $this->_delimiter;
  228. }
  229. /**
  230. * Set delimiter
  231. *
  232. * @param string $pValue Delimiter, defaults to ,
  233. * @return PHPExcel_Reader_CSV
  234. */
  235. public function setDelimiter($pValue = ',') {
  236. $this->_delimiter = $pValue;
  237. return $this;
  238. }
  239. /**
  240. * Get enclosure
  241. *
  242. * @return string
  243. */
  244. public function getEnclosure() {
  245. return $this->_enclosure;
  246. }
  247. /**
  248. * Set enclosure
  249. *
  250. * @param string $pValue Enclosure, defaults to "
  251. * @return PHPExcel_Reader_CSV
  252. */
  253. public function setEnclosure($pValue = '"') {
  254. if ($pValue == '') {
  255. $pValue = '"';
  256. }
  257. $this->_enclosure = $pValue;
  258. return $this;
  259. }
  260. /**
  261. * Get line ending
  262. *
  263. * @return string
  264. */
  265. public function getLineEnding() {
  266. return $this->_lineEnding;
  267. }
  268. /**
  269. * Set line ending
  270. *
  271. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  272. * @return PHPExcel_Reader_CSV
  273. */
  274. public function setLineEnding($pValue = PHP_EOL) {
  275. $this->_lineEnding = $pValue;
  276. return $this;
  277. }
  278. /**
  279. * Get sheet index
  280. *
  281. * @return int
  282. */
  283. public function getSheetIndex() {
  284. return $this->_sheetIndex;
  285. }
  286. /**
  287. * Set sheet index
  288. *
  289. * @param int $pValue Sheet index
  290. * @return PHPExcel_Reader_CSV
  291. */
  292. public function setSheetIndex($pValue = 0) {
  293. $this->_sheetIndex = $pValue;
  294. return $this;
  295. }
  296. }