PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/vendors/excel/PHPExcel/Writer/CSV.php

https://github.com/damanlovett/FTEP
PHP | 299 lines | 99 code | 36 blank | 164 comment | 10 complexity | 51c12d37078f066e4be0d3a958db847c MD5 | raw file
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2010 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
  23. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.3c, 2010-06-01
  26. */
  27. /**
  28. * PHPExcel_Writer_CSV
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer
  32. * @copyright Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
  35. /**
  36. * PHPExcel object
  37. *
  38. * @var PHPExcel
  39. */
  40. private $_phpExcel;
  41. /**
  42. * Delimiter
  43. *
  44. * @var string
  45. */
  46. private $_delimiter;
  47. /**
  48. * Enclosure
  49. *
  50. * @var string
  51. */
  52. private $_enclosure;
  53. /**
  54. * Line ending
  55. *
  56. * @var string
  57. */
  58. private $_lineEnding;
  59. /**
  60. * Sheet index to write
  61. *
  62. * @var int
  63. */
  64. private $_sheetIndex;
  65. /**
  66. * Pre-calculate formulas
  67. *
  68. * @var boolean
  69. */
  70. private $_preCalculateFormulas = true;
  71. /**
  72. * Whether to write a BOM (for UTF8).
  73. *
  74. * @var boolean
  75. */
  76. private $_useBOM = false;
  77. /**
  78. * Create a new PHPExcel_Writer_CSV
  79. *
  80. * @param PHPExcel $phpExcel PHPExcel object
  81. */
  82. public function __construct(PHPExcel $phpExcel) {
  83. $this->_phpExcel = $phpExcel;
  84. $this->_delimiter = ',';
  85. $this->_enclosure = '"';
  86. $this->_lineEnding = PHP_EOL;
  87. $this->_sheetIndex = 0;
  88. }
  89. /**
  90. * Save PHPExcel to file
  91. *
  92. * @param string $pFileName
  93. * @throws Exception
  94. */
  95. public function save($pFilename = null) {
  96. // Fetch sheet
  97. $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
  98. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  99. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  100. // Open file
  101. $fileHandle = fopen($pFilename, 'w');
  102. if ($fileHandle === false) {
  103. throw new Exception("Could not open file $pFilename for writing.");
  104. }
  105. if ($this->_useBOM) {
  106. // Write the UTF-8 BOM code
  107. fwrite($fileHandle, "\xEF\xBB\xBF");
  108. }
  109. // Convert sheet to array
  110. $cellsArray = $sheet->toArray('', $this->_preCalculateFormulas);
  111. // Write rows to file
  112. foreach ($cellsArray as $row) {
  113. $this->_writeLine($fileHandle, $row);
  114. }
  115. // Close file
  116. fclose($fileHandle);
  117. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  118. }
  119. /**
  120. * Get delimiter
  121. *
  122. * @return string
  123. */
  124. public function getDelimiter() {
  125. return $this->_delimiter;
  126. }
  127. /**
  128. * Set delimiter
  129. *
  130. * @param string $pValue Delimiter, defaults to ,
  131. * @return PHPExcel_Writer_CSV
  132. */
  133. public function setDelimiter($pValue = ',') {
  134. $this->_delimiter = $pValue;
  135. return $this;
  136. }
  137. /**
  138. * Get enclosure
  139. *
  140. * @return string
  141. */
  142. public function getEnclosure() {
  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. if ($pValue == '') {
  153. $pValue = null;
  154. }
  155. $this->_enclosure = $pValue;
  156. return $this;
  157. }
  158. /**
  159. * Get line ending
  160. *
  161. * @return string
  162. */
  163. public function getLineEnding() {
  164. return $this->_lineEnding;
  165. }
  166. /**
  167. * Set line ending
  168. *
  169. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  170. * @return PHPExcel_Writer_CSV
  171. */
  172. public function setLineEnding($pValue = PHP_EOL) {
  173. $this->_lineEnding = $pValue;
  174. return $this;
  175. }
  176. /**
  177. * Get whether BOM should be used
  178. *
  179. * @return boolean
  180. */
  181. public function getUseBOM() {
  182. return $this->_useBOM;
  183. }
  184. /**
  185. * Set whether BOM should be used
  186. *
  187. * @param boolean $pValue Use UTF-8 byte-order mark? Defaults to false
  188. * @return PHPExcel_Writer_CSV
  189. */
  190. public function setUseBOM($pValue = false) {
  191. $this->_useBOM = $pValue;
  192. return $this;
  193. }
  194. /**
  195. * Get sheet index
  196. *
  197. * @return int
  198. */
  199. public function getSheetIndex() {
  200. return $this->_sheetIndex;
  201. }
  202. /**
  203. * Set sheet index
  204. *
  205. * @param int $pValue Sheet index
  206. * @return PHPExcel_Writer_CSV
  207. */
  208. public function setSheetIndex($pValue = 0) {
  209. $this->_sheetIndex = $pValue;
  210. return $this;
  211. }
  212. /**
  213. * Write line to CSV file
  214. *
  215. * @param mixed $pFileHandle PHP filehandle
  216. * @param array $pValues Array containing values in a row
  217. * @throws Exception
  218. */
  219. private function _writeLine($pFileHandle = null, $pValues = null) {
  220. if (!is_null($pFileHandle) && is_array($pValues)) {
  221. // No leading delimiter
  222. $writeDelimiter = false;
  223. // Build the line
  224. $line = '';
  225. foreach ($pValues as $element) {
  226. // Escape enclosures
  227. $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
  228. // Add delimiter
  229. if ($writeDelimiter) {
  230. $line .= $this->_delimiter;
  231. } else {
  232. $writeDelimiter = true;
  233. }
  234. // Add enclosed string
  235. $line .= $this->_enclosure . $element . $this->_enclosure;
  236. }
  237. // Add line ending
  238. $line .= $this->_lineEnding;
  239. // Write to file
  240. fwrite($pFileHandle, $line);
  241. } else {
  242. throw new Exception("Invalid parameters passed.");
  243. }
  244. }
  245. /**
  246. * Get Pre-Calculate Formulas
  247. *
  248. * @return boolean
  249. */
  250. public function getPreCalculateFormulas() {
  251. return $this->_preCalculateFormulas;
  252. }
  253. /**
  254. * Set Pre-Calculate Formulas
  255. *
  256. * @param boolean $pValue Pre-Calculate Formulas?
  257. * @return PHPExcel_Writer_CSV
  258. */
  259. public function setPreCalculateFormulas($pValue = true) {
  260. $this->_preCalculateFormulas = $pValue;
  261. return $this;
  262. }
  263. }