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

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

#
PHP | 230 lines | 80 code | 28 blank | 122 comment | 11 complexity | a89b710916d01bc74ee654eb5199a2a0 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel */
  28. require_once 'PHPExcel.php';
  29. /** PHPExcel_Reader_IReader */
  30. require_once 'PHPExcel/Reader/IReader.php';
  31. /** PHPExcel_Worksheet */
  32. require_once 'PHPExcel/Worksheet.php';
  33. /** PHPExcel_Cell */
  34. require_once 'PHPExcel/Cell.php';
  35. /**
  36. * PHPExcel_Reader_CSV
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel_Reader
  40. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_Reader_CSV implements PHPExcel_Reader_IReader
  43. {
  44. /**
  45. * Delimiter
  46. *
  47. * @var string
  48. */
  49. private $_delimiter;
  50. /**
  51. * Enclosure
  52. *
  53. * @var string
  54. */
  55. private $_enclosure;
  56. /**
  57. * Line ending
  58. *
  59. * @var string
  60. */
  61. private $_lineEnding;
  62. /**
  63. * Sheet index to read
  64. *
  65. * @var int
  66. */
  67. private $_sheetIndex;
  68. /**
  69. * Create a new PHPExcel_Reader_CSV
  70. */
  71. public function __construct() {
  72. $this->_delimiter = ',';
  73. $this->_enclosure = '"';
  74. $this->_lineEnding = PHP_EOL;
  75. $this->_sheetIndex = 0;
  76. }
  77. /**
  78. * Loads PHPExcel from file
  79. *
  80. * @param string $pFilename
  81. * @throws Exception
  82. */
  83. public function load($pFilename)
  84. {
  85. // Create new PHPExcel
  86. $objPHPExcel = new PHPExcel();
  87. // Load into this instance
  88. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  89. }
  90. /**
  91. * Loads PHPExcel from file into PHPExcel instance
  92. *
  93. * @param string $pFilename
  94. * @param PHPExcel $objPHPExcel
  95. * @throws Exception
  96. */
  97. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  98. {
  99. // Check if file exists
  100. if (!file_exists($pFilename)) {
  101. throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
  102. }
  103. // Create new PHPExcel
  104. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
  105. $objPHPExcel->createSheet();
  106. }
  107. $objPHPExcel->setActiveSheetIndex( $this->_sheetIndex );
  108. // Open file
  109. $fileHandle = fopen($pFilename, 'r');
  110. if ($fileHandle === false) {
  111. throw new Exception("Could not open file $pFilename for reading.");
  112. }
  113. // Loop trough file
  114. $currentRow = 0;
  115. $rowData = array();
  116. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  117. $currentRow++;
  118. for ($i = 0; $i < count($rowData); $i++) {
  119. if ($rowData[$i] != '') {
  120. // Unescape enclosures
  121. $rowData[$i] = str_replace("\\" . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  122. $rowData[$i] = str_replace($this->_enclosure . $this->_enclosure, $this->_enclosure, $rowData[$i]);
  123. // Set cell value
  124. $objPHPExcel->getActiveSheet()->setCellValue(
  125. PHPExcel_Cell::stringFromColumnIndex($i) . $currentRow, $rowData[$i]
  126. );
  127. }
  128. }
  129. }
  130. // Close file
  131. fclose($fileHandle);
  132. // Return
  133. return $objPHPExcel;
  134. }
  135. /**
  136. * Get delimiter
  137. *
  138. * @return string
  139. */
  140. public function getDelimiter() {
  141. return $this->_delimiter;
  142. }
  143. /**
  144. * Set delimiter
  145. *
  146. * @param string $pValue Delimiter, defaults to ,
  147. */
  148. public function setDelimiter($pValue = ',') {
  149. $this->_delimiter = $pValue;
  150. }
  151. /**
  152. * Get enclosure
  153. *
  154. * @return string
  155. */
  156. public function getEnclosure() {
  157. return $this->_enclosure;
  158. }
  159. /**
  160. * Set enclosure
  161. *
  162. * @param string $pValue Enclosure, defaults to "
  163. */
  164. public function setEnclosure($pValue = '"') {
  165. if ($pValue == '') {
  166. $pValue = '"';
  167. }
  168. $this->_enclosure = $pValue;
  169. }
  170. /**
  171. * Get line ending
  172. *
  173. * @return string
  174. */
  175. public function getLineEnding() {
  176. return $this->_lineEnding;
  177. }
  178. /**
  179. * Set line ending
  180. *
  181. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  182. */
  183. public function setLineEnding($pValue = PHP_EOL) {
  184. $this->_lineEnding = $pValue;
  185. }
  186. /**
  187. * Get sheet index
  188. *
  189. * @return int
  190. */
  191. public function getSheetIndex() {
  192. return $this->_sheetIndex;
  193. }
  194. /**
  195. * Set sheet index
  196. *
  197. * @param int $pValue Sheet index
  198. */
  199. public function setSheetIndex($pValue = 0) {
  200. $this->_sheetIndex = $pValue;
  201. }
  202. }