PageRenderTime 77ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/v1.5.5/Classes/PHPExcel/Writer/CSV.php

#
PHP | 304 lines | 114 code | 39 blank | 151 comment | 20 complexity | a7280a5a67a51afe699c1641e56cc89b MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2007 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 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/lgpl.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. /** PHPExcel_IWriter */
  28. require_once 'PHPExcel/Writer/IWriter.php';
  29. /** PHPExcel_Cell */
  30. require_once 'PHPExcel/Cell.php';
  31. /** PHPExcel_RichText */
  32. require_once 'PHPExcel/RichText.php';
  33. /**
  34. * PHPExcel_Writer_CSV
  35. *
  36. * @category PHPExcel
  37. * @package PHPExcel_Writer
  38. * @copyright Copyright (c) 2006 - 2007 PHPExcel (http://www.codeplex.com/PHPExcel)
  39. */
  40. class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
  41. /**
  42. * PHPExcel object
  43. *
  44. * @var PHPExcel
  45. */
  46. private $_phpExcel;
  47. /**
  48. * Delimiter
  49. *
  50. * @var string
  51. */
  52. private $_delimiter;
  53. /**
  54. * Enclosure
  55. *
  56. * @var string
  57. */
  58. private $_enclosure;
  59. /**
  60. * Line ending
  61. *
  62. * @var string
  63. */
  64. private $_lineEnding;
  65. /**
  66. * Sheet index to write
  67. *
  68. * @var int
  69. */
  70. private $_sheetIndex;
  71. /**
  72. * Pre-calculate formulas
  73. *
  74. * @var boolean
  75. */
  76. private $_preCalculateFormulas = true;
  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. // Open file
  99. $fileHandle = fopen($pFilename, 'w');
  100. if ($fileHandle === false) {
  101. throw new Exception("Could not open file $pFilename for writing.");
  102. }
  103. // Get cell collection
  104. $cellCollection = $sheet->getCellCollection();
  105. // Get column count
  106. $colCount = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
  107. // Loop trough cells
  108. $currentRow = -1;
  109. $rowData = array();
  110. foreach ($cellCollection as $cell) {
  111. if ($currentRow != $cell->getRow()) {
  112. // End previous row?
  113. if ($currentRow != -1) {
  114. $this->_writeLine($fileHandle, $rowData);
  115. }
  116. // Set current row
  117. $currentRow = $cell->getRow();
  118. // Start a new row
  119. $rowData = array();
  120. for ($i = 0; $i < $colCount; $i++) {
  121. $rowData[$i] = '';
  122. }
  123. }
  124. // Copy cell
  125. $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1;
  126. if ($cell->getValue() instanceof PHPExcel_RichText) {
  127. $rowData[$column] = $cell->getValue()->getPlainText();
  128. } else {
  129. if ($this->_preCalculateFormulas) {
  130. $rowData[$column] = PHPExcel_Style_NumberFormat::toFormattedString(
  131. $cell->getCalculatedValue(),
  132. $sheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  133. );
  134. } else {
  135. $rowData[$column] = PHPExcel_Style_NumberFormat::ToFormattedString(
  136. $cell->getValue(),
  137. $sheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  138. );
  139. }
  140. }
  141. }
  142. // End last row?
  143. if ($currentRow != -1) {
  144. $this->_writeLine($fileHandle, $rowData);
  145. }
  146. // Close file
  147. fclose($fileHandle);
  148. }
  149. /**
  150. * Get delimiter
  151. *
  152. * @return string
  153. */
  154. public function getDelimiter() {
  155. return $this->_delimiter;
  156. }
  157. /**
  158. * Set delimiter
  159. *
  160. * @param string $pValue Delimiter, defaults to ,
  161. */
  162. public function setDelimiter($pValue = ',') {
  163. $this->_delimiter = $pValue;
  164. }
  165. /**
  166. * Get enclosure
  167. *
  168. * @return string
  169. */
  170. public function getEnclosure() {
  171. return $this->_enclosure;
  172. }
  173. /**
  174. * Set enclosure
  175. *
  176. * @param string $pValue Enclosure, defaults to "
  177. */
  178. public function setEnclosure($pValue = '"') {
  179. if ($pValue == '') {
  180. $pValue = null;
  181. }
  182. $this->_enclosure = $pValue;
  183. }
  184. /**
  185. * Get line ending
  186. *
  187. * @return string
  188. */
  189. public function getLineEnding() {
  190. return $this->_lineEnding;
  191. }
  192. /**
  193. * Set line ending
  194. *
  195. * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
  196. */
  197. public function setLineEnding($pValue = PHP_EOL) {
  198. $this->_lineEnding = $pValue;
  199. }
  200. /**
  201. * Get sheet index
  202. *
  203. * @return int
  204. */
  205. public function getSheetIndex() {
  206. return $this->_sheetIndex;
  207. }
  208. /**
  209. * Set sheet index
  210. *
  211. * @param int $pValue Sheet index
  212. */
  213. public function setSheetIndex($pValue = 0) {
  214. $this->_sheetIndex = $pValue;
  215. }
  216. /**
  217. * Write line to CSV file
  218. *
  219. * @param mixed $pFileHandle PHP filehandle
  220. * @param array $pValues Array containing values in a row
  221. * @throws Exception
  222. */
  223. private function _writeLine($pFileHandle = null, $pValues = null) {
  224. if (!is_null($pFileHandle) && is_array($pValues)) {
  225. // No leading delimiter
  226. $writeDelimiter = false;
  227. // Build the line
  228. $line = '';
  229. foreach ($pValues as $element) {
  230. // Escape enclosures
  231. $element = str_replace($this->_enclosure, "\\" . $this->_enclosure, $element);
  232. // Add delimiter
  233. if ($writeDelimiter) {
  234. $line .= $this->_delimiter;
  235. } else {
  236. $writeDelimiter = true;
  237. }
  238. // Add enclosed string
  239. $line .= $this->_enclosure . $element . $this->_enclosure;
  240. }
  241. // Add line ending
  242. $line .= $this->_lineEnding;
  243. // Write to file
  244. fwrite($pFileHandle, $line);
  245. } else {
  246. throw new Exception("Invalid parameters passed.");
  247. }
  248. }
  249. /**
  250. * Get Pre-Calculate Formulas
  251. *
  252. * @return boolean
  253. */
  254. public function getPreCalculateFormulas() {
  255. return $this->_preCalculateFormulas;
  256. }
  257. /**
  258. * Set Pre-Calculate Formulas
  259. *
  260. * @param boolean $pValue Pre-Calculate Formulas?
  261. */
  262. public function setPreCalculateFormulas($pValue = true) {
  263. $this->_preCalculateFormulas = $pValue;
  264. }
  265. }