4b201b4ffbb2ae0a8fd270d2990cc660b92e3ac1.svn-base 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 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 - 2014 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. * Sheet index to read
  67. *
  68. * @access private
  69. * @var int
  70. */
  71. private $_sheetIndex = 0;
  72. /**
  73. * Load rows contiguously
  74. *
  75. * @access private
  76. * @var int
  77. */
  78. private $_contiguous = false;
  79. /**
  80. * Row counter for loading rows contiguously
  81. *
  82. * @var int
  83. */
  84. private $_contiguousRow = -1;
  85. /**
  86. * Create a new PHPExcel_Reader_CSV
  87. */
  88. public function __construct() {
  89. $this->_readFilter = new PHPExcel_Reader_DefaultReadFilter();
  90. }
  91. /**
  92. * Validate that the current file is a CSV file
  93. *
  94. * @return boolean
  95. */
  96. protected function _isValidFormat()
  97. {
  98. return TRUE;
  99. }
  100. /**
  101. * Set input encoding
  102. *
  103. * @param string $pValue Input encoding
  104. */
  105. public function setInputEncoding($pValue = 'UTF-8')
  106. {
  107. $this->_inputEncoding = $pValue;
  108. return $this;
  109. }
  110. /**
  111. * Get input encoding
  112. *
  113. * @return string
  114. */
  115. public function getInputEncoding()
  116. {
  117. return $this->_inputEncoding;
  118. }
  119. /**
  120. * Move filepointer past any BOM marker
  121. *
  122. */
  123. protected function _skipBOM()
  124. {
  125. rewind($this->_fileHandle);
  126. switch ($this->_inputEncoding) {
  127. case 'UTF-8':
  128. fgets($this->_fileHandle, 4) == "\xEF\xBB\xBF" ?
  129. fseek($this->_fileHandle, 3) : fseek($this->_fileHandle, 0);
  130. break;
  131. case 'UTF-16LE':
  132. fgets($this->_fileHandle, 3) == "\xFF\xFE" ?
  133. fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
  134. break;
  135. case 'UTF-16BE':
  136. fgets($this->_fileHandle, 3) == "\xFE\xFF" ?
  137. fseek($this->_fileHandle, 2) : fseek($this->_fileHandle, 0);
  138. break;
  139. case 'UTF-32LE':
  140. fgets($this->_fileHandle, 5) == "\xFF\xFE\x00\x00" ?
  141. fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
  142. break;
  143. case 'UTF-32BE':
  144. fgets($this->_fileHandle, 5) == "\x00\x00\xFE\xFF" ?
  145. fseek($this->_fileHandle, 4) : fseek($this->_fileHandle, 0);
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. /**
  152. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  153. *
  154. * @param string $pFilename
  155. * @throws PHPExcel_Reader_Exception
  156. */
  157. public function listWorksheetInfo($pFilename)
  158. {
  159. // Open file
  160. $this->_openFile($pFilename);
  161. if (!$this->_isValidFormat()) {
  162. fclose ($this->_fileHandle);
  163. throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
  164. }
  165. $fileHandle = $this->_fileHandle;
  166. // Skip BOM, if any
  167. $this->_skipBOM();
  168. $escapeEnclosures = array( "\\" . $this->_enclosure, $this->_enclosure . $this->_enclosure );
  169. $worksheetInfo = array();
  170. $worksheetInfo[0]['worksheetName'] = 'Worksheet';
  171. $worksheetInfo[0]['lastColumnLetter'] = 'A';
  172. $worksheetInfo[0]['lastColumnIndex'] = 0;
  173. $worksheetInfo[0]['totalRows'] = 0;
  174. $worksheetInfo[0]['totalColumns'] = 0;
  175. // Loop through each line of the file in turn
  176. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  177. $worksheetInfo[0]['totalRows']++;
  178. $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
  179. }
  180. $worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
  181. $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
  182. // Close file
  183. fclose($fileHandle);
  184. return $worksheetInfo;
  185. }
  186. /**
  187. * Loads PHPExcel from file
  188. *
  189. * @param string $pFilename
  190. * @return PHPExcel
  191. * @throws PHPExcel_Reader_Exception
  192. */
  193. public function load($pFilename)
  194. {
  195. // Create new PHPExcel
  196. $objPHPExcel = new PHPExcel();
  197. // Load into this instance
  198. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  199. }
  200. /**
  201. * Loads PHPExcel from file into PHPExcel instance
  202. *
  203. * @param string $pFilename
  204. * @param PHPExcel $objPHPExcel
  205. * @return PHPExcel
  206. * @throws PHPExcel_Reader_Exception
  207. */
  208. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  209. {
  210. $lineEnding = ini_get('auto_detect_line_endings');
  211. ini_set('auto_detect_line_endings', true);
  212. // Open file
  213. $this->_openFile($pFilename);
  214. if (!$this->_isValidFormat()) {
  215. fclose ($this->_fileHandle);
  216. throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
  217. }
  218. $fileHandle = $this->_fileHandle;
  219. // Skip BOM, if any
  220. $this->_skipBOM();
  221. // Create new PHPExcel object
  222. while ($objPHPExcel->getSheetCount() <= $this->_sheetIndex) {
  223. $objPHPExcel->createSheet();
  224. }
  225. $sheet = $objPHPExcel->setActiveSheetIndex($this->_sheetIndex);
  226. $escapeEnclosures = array( "\\" . $this->_enclosure,
  227. $this->_enclosure . $this->_enclosure
  228. );
  229. // Set our starting row based on whether we're in contiguous mode or not
  230. $currentRow = 1;
  231. if ($this->_contiguous) {
  232. $currentRow = ($this->_contiguousRow == -1) ? $sheet->getHighestRow(): $this->_contiguousRow;
  233. }
  234. // Loop through each line of the file in turn
  235. while (($rowData = fgetcsv($fileHandle, 0, $this->_delimiter, $this->_enclosure)) !== FALSE) {
  236. $columnLetter = 'A';
  237. foreach($rowData as $rowDatum) {
  238. if ($rowDatum != '' && $this->_readFilter->readCell($columnLetter, $currentRow)) {
  239. // Unescape enclosures
  240. $rowDatum = str_replace($escapeEnclosures, $this->_enclosure, $rowDatum);
  241. // Convert encoding if necessary
  242. if ($this->_inputEncoding !== 'UTF-8') {
  243. $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->_inputEncoding);
  244. }
  245. // Set cell value
  246. $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
  247. }
  248. ++$columnLetter;
  249. }
  250. ++$currentRow;
  251. }
  252. // Close file
  253. fclose($fileHandle);
  254. if ($this->_contiguous) {
  255. $this->_contiguousRow = $currentRow;
  256. }
  257. ini_set('auto_detect_line_endings', $lineEnding);
  258. // Return
  259. return $objPHPExcel;
  260. }
  261. /**
  262. * Get delimiter
  263. *
  264. * @return string
  265. */
  266. public function getDelimiter() {
  267. return $this->_delimiter;
  268. }
  269. /**
  270. * Set delimiter
  271. *
  272. * @param string $pValue Delimiter, defaults to ,
  273. * @return PHPExcel_Reader_CSV
  274. */
  275. public function setDelimiter($pValue = ',') {
  276. $this->_delimiter = $pValue;
  277. return $this;
  278. }
  279. /**
  280. * Get enclosure
  281. *
  282. * @return string
  283. */
  284. public function getEnclosure() {
  285. return $this->_enclosure;
  286. }
  287. /**
  288. * Set enclosure
  289. *
  290. * @param string $pValue Enclosure, defaults to "
  291. * @return PHPExcel_Reader_CSV
  292. */
  293. public function setEnclosure($pValue = '"') {
  294. if ($pValue == '') {
  295. $pValue = '"';
  296. }
  297. $this->_enclosure = $pValue;
  298. return $this;
  299. }
  300. /**
  301. * Get sheet index
  302. *
  303. * @return integer
  304. */
  305. public function getSheetIndex() {
  306. return $this->_sheetIndex;
  307. }
  308. /**
  309. * Set sheet index
  310. *
  311. * @param integer $pValue Sheet index
  312. * @return PHPExcel_Reader_CSV
  313. */
  314. public function setSheetIndex($pValue = 0) {
  315. $this->_sheetIndex = $pValue;
  316. return $this;
  317. }
  318. /**
  319. * Set Contiguous
  320. *
  321. * @param boolean $contiguous
  322. */
  323. public function setContiguous($contiguous = FALSE)
  324. {
  325. $this->_contiguous = (bool) $contiguous;
  326. if (!$contiguous) {
  327. $this->_contiguousRow = -1;
  328. }
  329. return $this;
  330. }
  331. /**
  332. * Get Contiguous
  333. *
  334. * @return boolean
  335. */
  336. public function getContiguous() {
  337. return $this->_contiguous;
  338. }
  339. }