CSV.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. if (!defined('PHPEXCEL_ROOT')) {
  8. /**
  9. * @ignore
  10. */
  11. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
  12. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  13. }
  14. /**
  15. * PHPExcel_Reader_CSV
  16. *
  17. * Copyright (c) 2006 - 2015 PHPExcel
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2.1 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library; if not, write to the Free Software
  31. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  32. *
  33. * @category PHPExcel
  34. * @package PHPExcel_Reader
  35. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  36. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  37. * @version ##VERSION##, ##DATE##
  38. */
  39. class PHPExcel_Reader_CSV extends PHPExcel_Reader_Abstract implements PHPExcel_Reader_IReader
  40. {
  41. /**
  42. * Input encoding
  43. *
  44. * @access private
  45. * @var string
  46. */
  47. private $inputEncoding = 'UTF-8';
  48. /**
  49. * Delimiter
  50. *
  51. * @access private
  52. * @var string
  53. */
  54. private $delimiter = ',';
  55. /**
  56. * Enclosure
  57. *
  58. * @access private
  59. * @var string
  60. */
  61. private $enclosure = '"';
  62. /**
  63. * Sheet index to read
  64. *
  65. * @access private
  66. * @var int
  67. */
  68. private $sheetIndex = 0;
  69. /**
  70. * Load rows contiguously
  71. *
  72. * @access private
  73. * @var int
  74. */
  75. private $contiguous = false;
  76. /**
  77. * Row counter for loading rows contiguously
  78. *
  79. * @var int
  80. */
  81. private $contiguousRow = -1;
  82. /**
  83. * Create a new PHPExcel_Reader_CSV
  84. */
  85. public function __construct()
  86. {
  87. $this->readFilter = new PHPExcel_Reader_DefaultReadFilter();
  88. }
  89. /**
  90. * Validate that the current file is a CSV file
  91. *
  92. * @return boolean
  93. */
  94. protected function isValidFormat()
  95. {
  96. return true;
  97. }
  98. /**
  99. * Set input encoding
  100. *
  101. * @param string $pValue Input encoding
  102. */
  103. public function setInputEncoding($pValue = 'UTF-8')
  104. {
  105. $this->inputEncoding = $pValue;
  106. return $this;
  107. }
  108. /**
  109. * Get input encoding
  110. *
  111. * @return string
  112. */
  113. public function getInputEncoding()
  114. {
  115. return $this->inputEncoding;
  116. }
  117. /**
  118. * Move filepointer past any BOM marker
  119. *
  120. */
  121. protected function skipBOM()
  122. {
  123. rewind($this->fileHandle);
  124. switch ($this->inputEncoding) {
  125. case 'UTF-8':
  126. fgets($this->fileHandle, 4) == "\xEF\xBB\xBF" ?
  127. fseek($this->fileHandle, 3) : fseek($this->fileHandle, 0);
  128. break;
  129. case 'UTF-16LE':
  130. fgets($this->fileHandle, 3) == "\xFF\xFE" ?
  131. fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0);
  132. break;
  133. case 'UTF-16BE':
  134. fgets($this->fileHandle, 3) == "\xFE\xFF" ?
  135. fseek($this->fileHandle, 2) : fseek($this->fileHandle, 0);
  136. break;
  137. case 'UTF-32LE':
  138. fgets($this->fileHandle, 5) == "\xFF\xFE\x00\x00" ?
  139. fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0);
  140. break;
  141. case 'UTF-32BE':
  142. fgets($this->fileHandle, 5) == "\x00\x00\xFE\xFF" ?
  143. fseek($this->fileHandle, 4) : fseek($this->fileHandle, 0);
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. /**
  150. * Identify any separator that is explicitly set in the file
  151. *
  152. */
  153. protected function checkSeparator()
  154. {
  155. $line = fgets($this->fileHandle);
  156. if ($line === false) {
  157. return;
  158. }
  159. if ((strlen(trim($line, "\r\n")) == 5) && (stripos($line, 'sep=') === 0)) {
  160. $this->delimiter = substr($line, 4, 1);
  161. return;
  162. }
  163. return $this->skipBOM();
  164. }
  165. /**
  166. * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns)
  167. *
  168. * @param string $pFilename
  169. * @throws PHPExcel_Reader_Exception
  170. */
  171. public function listWorksheetInfo($pFilename)
  172. {
  173. // Open file
  174. $this->openFile($pFilename);
  175. if (!$this->isValidFormat()) {
  176. fclose($this->fileHandle);
  177. throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
  178. }
  179. $fileHandle = $this->fileHandle;
  180. // Skip BOM, if any
  181. $this->skipBOM();
  182. $this->checkSeparator();
  183. $escapeEnclosures = array( "\\" . $this->enclosure, $this->enclosure . $this->enclosure );
  184. $worksheetInfo = array();
  185. $worksheetInfo[0]['worksheetName'] = 'Worksheet';
  186. $worksheetInfo[0]['lastColumnLetter'] = 'A';
  187. $worksheetInfo[0]['lastColumnIndex'] = 0;
  188. $worksheetInfo[0]['totalRows'] = 0;
  189. $worksheetInfo[0]['totalColumns'] = 0;
  190. // Loop through each line of the file in turn
  191. while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) {
  192. $worksheetInfo[0]['totalRows']++;
  193. $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
  194. }
  195. $worksheetInfo[0]['lastColumnLetter'] = PHPExcel_Cell::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex']);
  196. $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1;
  197. // Close file
  198. fclose($fileHandle);
  199. return $worksheetInfo;
  200. }
  201. /**
  202. * Loads PHPExcel from file
  203. *
  204. * @param string $pFilename
  205. * @return PHPExcel
  206. * @throws PHPExcel_Reader_Exception
  207. */
  208. public function load($pFilename)
  209. {
  210. // Create new PHPExcel
  211. $objPHPExcel = new PHPExcel();
  212. // Load into this instance
  213. return $this->loadIntoExisting($pFilename, $objPHPExcel);
  214. }
  215. /**
  216. * Loads PHPExcel from file into PHPExcel instance
  217. *
  218. * @param string $pFilename
  219. * @param PHPExcel $objPHPExcel
  220. * @return PHPExcel
  221. * @throws PHPExcel_Reader_Exception
  222. */
  223. public function loadIntoExisting($pFilename, PHPExcel $objPHPExcel)
  224. {
  225. $lineEnding = ini_get('auto_detect_line_endings');
  226. ini_set('auto_detect_line_endings', true);
  227. // Open file
  228. $this->openFile($pFilename);
  229. if (!$this->isValidFormat()) {
  230. fclose($this->fileHandle);
  231. throw new PHPExcel_Reader_Exception($pFilename . " is an Invalid Spreadsheet file.");
  232. }
  233. $fileHandle = $this->fileHandle;
  234. // Skip BOM, if any
  235. $this->skipBOM();
  236. $this->checkSeparator();
  237. // Create new PHPExcel object
  238. while ($objPHPExcel->getSheetCount() <= $this->sheetIndex) {
  239. $objPHPExcel->createSheet();
  240. }
  241. $sheet = $objPHPExcel->setActiveSheetIndex($this->sheetIndex);
  242. $escapeEnclosures = array( "\\" . $this->enclosure,
  243. $this->enclosure . $this->enclosure
  244. );
  245. // Set our starting row based on whether we're in contiguous mode or not
  246. $currentRow = 1;
  247. if ($this->contiguous) {
  248. $currentRow = ($this->contiguousRow == -1) ? $sheet->getHighestRow(): $this->contiguousRow;
  249. }
  250. // Loop through each line of the file in turn
  251. while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) {
  252. $columnLetter = 'A';
  253. foreach ($rowData as $rowDatum) {
  254. if ($rowDatum != '' && $this->readFilter->readCell($columnLetter, $currentRow)) {
  255. // Unescape enclosures
  256. $rowDatum = str_replace($escapeEnclosures, $this->enclosure, $rowDatum);
  257. // Convert encoding if necessary
  258. if ($this->inputEncoding !== 'UTF-8') {
  259. $rowDatum = PHPExcel_Shared_String::ConvertEncoding($rowDatum, 'UTF-8', $this->inputEncoding);
  260. }
  261. // Set cell value
  262. $sheet->getCell($columnLetter . $currentRow)->setValue($rowDatum);
  263. }
  264. ++$columnLetter;
  265. }
  266. ++$currentRow;
  267. }
  268. // Close file
  269. fclose($fileHandle);
  270. if ($this->contiguous) {
  271. $this->contiguousRow = $currentRow;
  272. }
  273. ini_set('auto_detect_line_endings', $lineEnding);
  274. // Return
  275. return $objPHPExcel;
  276. }
  277. /**
  278. * Get delimiter
  279. *
  280. * @return string
  281. */
  282. public function getDelimiter()
  283. {
  284. return $this->delimiter;
  285. }
  286. /**
  287. * Set delimiter
  288. *
  289. * @param string $pValue Delimiter, defaults to ,
  290. * @return PHPExcel_Reader_CSV
  291. */
  292. public function setDelimiter($pValue = ',')
  293. {
  294. $this->delimiter = $pValue;
  295. return $this;
  296. }
  297. /**
  298. * Get enclosure
  299. *
  300. * @return string
  301. */
  302. public function getEnclosure()
  303. {
  304. return $this->enclosure;
  305. }
  306. /**
  307. * Set enclosure
  308. *
  309. * @param string $pValue Enclosure, defaults to "
  310. * @return PHPExcel_Reader_CSV
  311. */
  312. public function setEnclosure($pValue = '"')
  313. {
  314. if ($pValue == '') {
  315. $pValue = '"';
  316. }
  317. $this->enclosure = $pValue;
  318. return $this;
  319. }
  320. /**
  321. * Get sheet index
  322. *
  323. * @return integer
  324. */
  325. public function getSheetIndex()
  326. {
  327. return $this->sheetIndex;
  328. }
  329. /**
  330. * Set sheet index
  331. *
  332. * @param integer $pValue Sheet index
  333. * @return PHPExcel_Reader_CSV
  334. */
  335. public function setSheetIndex($pValue = 0)
  336. {
  337. $this->sheetIndex = $pValue;
  338. return $this;
  339. }
  340. /**
  341. * Set Contiguous
  342. *
  343. * @param boolean $contiguous
  344. */
  345. public function setContiguous($contiguous = false)
  346. {
  347. $this->contiguous = (bool) $contiguous;
  348. if (!$contiguous) {
  349. $this->contiguousRow = -1;
  350. }
  351. return $this;
  352. }
  353. /**
  354. * Get Contiguous
  355. *
  356. * @return boolean
  357. */
  358. public function getContiguous()
  359. {
  360. return $this->contiguous;
  361. }
  362. }