/protected/libs/phpexcel/Classes/PHPExcel/Writer/CSV.php

https://github.com/allinside/Yii-CMS · PHP · 298 lines · 98 code · 36 blank · 164 comment · 9 complexity · 4ba64decfc2bee7208f50441da13a0ea MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  26. */
  27. /**
  28. * PHPExcel_Writer_CSV
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Writer
  32. * @copyright Copyright (c) 2006 - 2011 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 = PHP_EOL;
  59. /**
  60. * Sheet index to write
  61. *
  62. * @var int
  63. */
  64. private $_sheetIndex = 0;
  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. }
  85. /**
  86. * Save PHPExcel to file
  87. *
  88. * @param string $pFileName
  89. * @throws Exception
  90. */
  91. public function save($pFilename = null) {
  92. // Fetch sheet
  93. $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
  94. $saveDebugLog = PHPExcel_Calculation::getInstance()->writeDebugLog;
  95. PHPExcel_Calculation::getInstance()->writeDebugLog = false;
  96. $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
  97. PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  98. // Open file
  99. $fileHandle = fopen($pFilename, 'wb+');
  100. if ($fileHandle === false) {
  101. throw new Exception("Could not open file $pFilename for writing.");
  102. }
  103. if ($this->_useBOM) {
  104. // Write the UTF-8 BOM code
  105. fwrite($fileHandle, "\xEF\xBB\xBF");
  106. }
  107. // Convert sheet to array
  108. $cellsArray = $sheet->toArray('', $this->_preCalculateFormulas);
  109. // Write rows to file
  110. foreach ($cellsArray as $row) {
  111. $this->_writeLine($fileHandle, $row);
  112. }
  113. // Close file
  114. fclose($fileHandle);
  115. PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  116. PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
  117. }
  118. /**
  119. * Get delimiter
  120. *
  121. * @return string
  122. */
  123. public function getDelimiter() {
  124. return $this->_delimiter;
  125. }
  126. /**
  127. * Set delimiter
  128. *
  129. * @param string $pValue Delimiter, defaults to ,
  130. * @return PHPExcel_Writer_CSV
  131. */
  132. public function setDelimiter($pValue = ',') {
  133. $this->_delimiter = $pValue;
  134. return $this;
  135. }
  136. /**
  137. * Get enclosure
  138. *
  139. * @return string
  140. */
  141. public function getEnclosure() {
  142. return $this->_enclosure;
  143. }
  144. /**
  145. * Set enclosure
  146. *
  147. * @param string $pValue Enclosure, defaults to "
  148. * @return PHPExcel_Writer_CSV
  149. */
  150. public function setEnclosure($pValue = '"') {
  151. if ($pValue == '') {
  152. $pValue = null;
  153. }
  154. $this->_enclosure = $pValue;
  155. return $this;
  156. }
  157. /**
  158. * Get line ending
  159. *
  160. * @return string
  161. */
  162. public function getLineEnding() {
  163. return $this->_lineEnding;
  164. }
  165. /**
  166. * Set line ending
  167. *
  168. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  169. * @return PHPExcel_Writer_CSV
  170. */
  171. public function setLineEnding($pValue = PHP_EOL) {
  172. $this->_lineEnding = $pValue;
  173. return $this;
  174. }
  175. /**
  176. * Get whether BOM should be used
  177. *
  178. * @return boolean
  179. */
  180. public function getUseBOM() {
  181. return $this->_useBOM;
  182. }
  183. /**
  184. * Set whether BOM should be used
  185. *
  186. * @param boolean $pValue Use UTF-8 byte-order mark? Defaults to false
  187. * @return PHPExcel_Writer_CSV
  188. */
  189. public function setUseBOM($pValue = false) {
  190. $this->_useBOM = $pValue;
  191. return $this;
  192. }
  193. /**
  194. * Get sheet index
  195. *
  196. * @return int
  197. */
  198. public function getSheetIndex() {
  199. return $this->_sheetIndex;
  200. }
  201. /**
  202. * Set sheet index
  203. *
  204. * @param int $pValue Sheet index
  205. * @return PHPExcel_Writer_CSV
  206. */
  207. public function setSheetIndex($pValue = 0) {
  208. $this->_sheetIndex = $pValue;
  209. return $this;
  210. }
  211. /**
  212. * Write line to CSV file
  213. *
  214. * @param mixed $pFileHandle PHP filehandle
  215. * @param array $pValues Array containing values in a row
  216. * @throws Exception
  217. */
  218. private function _writeLine($pFileHandle = null, $pValues = null) {
  219. if (is_array($pValues)) {
  220. // No leading delimiter
  221. $writeDelimiter = false;
  222. // Build the line
  223. $line = '';
  224. foreach ($pValues as $element) {
  225. // Escape enclosures
  226. $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
  227. // Add delimiter
  228. if ($writeDelimiter) {
  229. $line .= $this->_delimiter;
  230. } else {
  231. $writeDelimiter = true;
  232. }
  233. // Add enclosed string
  234. $line .= $this->_enclosure . $element . $this->_enclosure;
  235. }
  236. // Add line ending
  237. $line .= $this->_lineEnding;
  238. // Write to file
  239. fwrite($pFileHandle, $line);
  240. } else {
  241. throw new Exception("Invalid parameters passed.");
  242. }
  243. }
  244. /**
  245. * Get Pre-Calculate Formulas
  246. *
  247. * @return boolean
  248. */
  249. public function getPreCalculateFormulas() {
  250. return $this->_preCalculateFormulas;
  251. }
  252. /**
  253. * Set Pre-Calculate Formulas
  254. *
  255. * @param boolean $pValue Pre-Calculate Formulas?
  256. * @return PHPExcel_Writer_CSV
  257. */
  258. public function setPreCalculateFormulas($pValue = true) {
  259. $this->_preCalculateFormulas = $pValue;
  260. return $this;
  261. }
  262. }