For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
9.2 KiB

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