CSV.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  8. {
  9. /**
  10. * PHPExcel object
  11. *
  12. * @var PHPExcel
  13. */
  14. private $phpExcel;
  15. /**
  16. * Delimiter
  17. *
  18. * @var string
  19. */
  20. private $delimiter = ',';
  21. /**
  22. * Enclosure
  23. *
  24. * @var string
  25. */
  26. private $enclosure = '"';
  27. /**
  28. * Line ending
  29. *
  30. * @var string
  31. */
  32. private $lineEnding = PHP_EOL;
  33. /**
  34. * Sheet index to write
  35. *
  36. * @var int
  37. */
  38. private $sheetIndex = 0;
  39. /**
  40. * Whether to write a BOM (for UTF8).
  41. *
  42. * @var boolean
  43. */
  44. private $useBOM = false;
  45. /**
  46. * Whether to write a Separator line as the first line of the file
  47. * sep=x
  48. *
  49. * @var boolean
  50. */
  51. private $includeSeparatorLine = false;
  52. /**
  53. * Whether to write a fully Excel compatible CSV file.
  54. *
  55. * @var boolean
  56. */
  57. private $excelCompatibility = false;
  58. /**
  59. * Create a new PHPExcel_Writer_CSV
  60. *
  61. * @param PHPExcel $phpExcel PHPExcel object
  62. */
  63. public function __construct(PHPExcel $phpExcel)
  64. {
  65. $this->phpExcel = $phpExcel;
  66. }
  67. /**
  68. * Save PHPExcel to file
  69. *
  70. * @param string $pFilename
  71. * @throws PHPExcel_Writer_Exception
  72. */
  73. public function save($pFilename = null)
  74. {
  75. // Fetch sheet
  76. $sheet = $this->phpExcel->getSheet($this->sheetIndex);
  77. $saveDebugLog = PHPExcel_Calculation::getInstance($this->phpExcel)->getDebugLog()->getWriteDebugLog();
  78. PHPExcel_Calculation::getInstance($this->phpExcel)->getDebugLog()->setWriteDebugLog(false);
  79. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  80. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  81. // Open file
  82. $fileHandle = fopen($pFilename, 'wb+');
  83. if ($fileHandle === false) {
  84. throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
  85. }
  86. if ($this->excelCompatibility) {
  87. $this->setUseBOM(true); // Enforce UTF-8 BOM Header
  88. $this->setIncludeSeparatorLine(true); // Set separator line
  89. $this->setEnclosure('"'); // Set enclosure to "
  90. $this->setDelimiter(";"); // Set delimiter to a semi-colon
  91. $this->setLineEnding("\r\n");
  92. }
  93. if ($this->useBOM) {
  94. // Write the UTF-8 BOM code if required
  95. fwrite($fileHandle, "\xEF\xBB\xBF");
  96. }
  97. if ($this->includeSeparatorLine) {
  98. // Write the separator line if required
  99. fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
  100. }
  101. // Identify the range that we need to extract from the worksheet
  102. $maxCol = $sheet->getHighestDataColumn();
  103. $maxRow = $sheet->getHighestDataRow();
  104. // Write rows to file
  105. for ($row = 1; $row <= $maxRow; ++$row) {
  106. // Convert the row to an array...
  107. $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row, '', $this->preCalculateFormulas);
  108. // ... and write to the file
  109. $this->writeLine($fileHandle, $cellsArray[0]);
  110. }
  111. // Close file
  112. fclose($fileHandle);
  113. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  114. PHPExcel_Calculation::getInstance($this->phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
  115. }
  116. /**
  117. * Get delimiter
  118. *
  119. * @return string
  120. */
  121. public function getDelimiter()
  122. {
  123. return $this->delimiter;
  124. }
  125. /**
  126. * Set delimiter
  127. *
  128. * @param string $pValue Delimiter, defaults to ,
  129. * @return PHPExcel_Writer_CSV
  130. */
  131. public function setDelimiter($pValue = ',')
  132. {
  133. $this->delimiter = $pValue;
  134. return $this;
  135. }
  136. /**
  137. * Get enclosure
  138. *
  139. * @return string
  140. */
  141. public function getEnclosure()
  142. {
  143. return $this->enclosure;
  144. }
  145. /**
  146. * Set enclosure
  147. *
  148. * @param string $pValue Enclosure, defaults to "
  149. * @return PHPExcel_Writer_CSV
  150. */
  151. public function setEnclosure($pValue = '"')
  152. {
  153. if ($pValue == '') {
  154. $pValue = null;
  155. }
  156. $this->enclosure = $pValue;
  157. return $this;
  158. }
  159. /**
  160. * Get line ending
  161. *
  162. * @return string
  163. */
  164. public function getLineEnding()
  165. {
  166. return $this->lineEnding;
  167. }
  168. /**
  169. * Set line ending
  170. *
  171. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  172. * @return PHPExcel_Writer_CSV
  173. */
  174. public function setLineEnding($pValue = PHP_EOL)
  175. {
  176. $this->lineEnding = $pValue;
  177. return $this;
  178. }
  179. /**
  180. * Get whether BOM should be used
  181. *
  182. * @return boolean
  183. */
  184. public function getUseBOM()
  185. {
  186. return $this->useBOM;
  187. }
  188. /**
  189. * Set whether BOM should be used
  190. *
  191. * @param boolean $pValue Use UTF-8 byte-order mark? Defaults to false
  192. * @return PHPExcel_Writer_CSV
  193. */
  194. public function setUseBOM($pValue = false)
  195. {
  196. $this->useBOM = $pValue;
  197. return $this;
  198. }
  199. /**
  200. * Get whether a separator line should be included
  201. *
  202. * @return boolean
  203. */
  204. public function getIncludeSeparatorLine()
  205. {
  206. return $this->includeSeparatorLine;
  207. }
  208. /**
  209. * Set whether a separator line should be included as the first line of the file
  210. *
  211. * @param boolean $pValue Use separator line? Defaults to false
  212. * @return PHPExcel_Writer_CSV
  213. */
  214. public function setIncludeSeparatorLine($pValue = false)
  215. {
  216. $this->includeSeparatorLine = $pValue;
  217. return $this;
  218. }
  219. /**
  220. * Get whether the file should be saved with full Excel Compatibility
  221. *
  222. * @return boolean
  223. */
  224. public function getExcelCompatibility()
  225. {
  226. return $this->excelCompatibility;
  227. }
  228. /**
  229. * Set whether the file should be saved with full Excel Compatibility
  230. *
  231. * @param boolean $pValue Set the file to be written as a fully Excel compatible csv file
  232. * Note that this overrides other settings such as useBOM, enclosure and delimiter
  233. * @return PHPExcel_Writer_CSV
  234. */
  235. public function setExcelCompatibility($pValue = false)
  236. {
  237. $this->excelCompatibility = $pValue;
  238. return $this;
  239. }
  240. /**
  241. * Get sheet index
  242. *
  243. * @return int
  244. */
  245. public function getSheetIndex()
  246. {
  247. return $this->sheetIndex;
  248. }
  249. /**
  250. * Set sheet index
  251. *
  252. * @param int $pValue Sheet index
  253. * @return PHPExcel_Writer_CSV
  254. */
  255. public function setSheetIndex($pValue = 0)
  256. {
  257. $this->sheetIndex = $pValue;
  258. return $this;
  259. }
  260. /**
  261. * Write line to CSV file
  262. *
  263. * @param mixed $pFileHandle PHP filehandle
  264. * @param array $pValues Array containing values in a row
  265. * @throws PHPExcel_Writer_Exception
  266. */
  267. private function writeLine($pFileHandle = null, $pValues = null)
  268. {
  269. if (is_array($pValues)) {
  270. // No leading delimiter
  271. $writeDelimiter = false;
  272. // Build the line
  273. $line = '';
  274. foreach ($pValues as $element) {
  275. // Escape enclosures
  276. $element = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $element);
  277. // Add delimiter
  278. if ($writeDelimiter) {
  279. $line .= $this->delimiter;
  280. } else {
  281. $writeDelimiter = true;
  282. }
  283. // Add enclosed string
  284. $line .= $this->enclosure . $element . $this->enclosure;
  285. }
  286. // Add line ending
  287. $line .= $this->lineEnding;
  288. // Write to file
  289. fwrite($pFileHandle, $line);
  290. } else {
  291. throw new PHPExcel_Writer_Exception("Invalid data row passed to CSV writer.");
  292. }
  293. }
  294. }