File Source for CSV.php

PHPExcel_Writer

# Source for file CSV.php

Documentation is available at CSV.php

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. */
29. /**
30. * PHPExcel_Writer_CSV
31. *
32. * @category PHPExcel
33. * @package PHPExcel_Writer
34. * @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
35. */
36. class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
37. /**
38. * PHPExcel object
39. *
40. * @var PHPExcel
41. */
42. private $_phpExcel;
44. /**
45. * Delimiter
46. *
47. * @var string
48. */
49. private $_delimiter = ',';
51. /**
52. * Enclosure
53. *
54. * @var string
55. */
56. private $_enclosure = '"';
58. /**
59. * Line ending
60. *
61. * @var string
62. */
63. private $_lineEnding = PHP_EOL;
65. /**
66. * Sheet index to write
67. *
68. * @var int
69. */
70. private $_sheetIndex = 0;
72. /**
73. * Pre-calculate formulas
74. *
75. * @var boolean
76. */
77. private $_preCalculateFormulas = true;
79. /**
80. * Whether to write a BOM (for UTF8).
81. *
82. * @var boolean
83. */
84. private $_useBOM = false;
86. /**
87. * Create a new PHPExcel_Writer_CSV
88. *
89. * @param PHPExcel $phpExcel PHPExcel object
90. */
91. public function __construct(PHPExcel $phpExcel) {
92. $this->_phpExcel = $phpExcel;
93. }
95. /**
96. * Save PHPExcel to file
97. *
98. * @param string $pFileName
99. * @throws Exception
100. */
101. public function save($pFilename = null) {
102. // Fetch sheet
103. $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
105. $saveDebugLog = PHPExcel_Calculation:: getInstance()->writeDebugLog;
106. PHPExcel_Calculation:: getInstance()->writeDebugLog = false;
107. $saveArrayReturnType = PHPExcel_Calculation:: getArrayReturnType();
108. PHPExcel_Calculation:: setArrayReturnType( PHPExcel_Calculation:: RETURN_ARRAY_AS_VALUE);
110. // Open file
111. $fileHandle = fopen($pFilename, 'wb+');
112. if ($fileHandle === false) {
113. throw new Exception("Could not open file $pFilename for writing.");
114. }
116. if ($this->_useBOM) {
117. // Write the UTF-8 BOM code
118. fwrite($fileHandle, "\xEF\xBB\xBF");
119. }
121. // Convert sheet to array
122. $cellsArray = $sheet->toArray('', $this->_preCalculateFormulas);
124. // Write rows to file
125. foreach ($cellsArray as $row) {
126. $this->_writeLine($fileHandle, $row);
127. }
129. // Close file
130. fclose($fileHandle);
132. PHPExcel_Calculation:: setArrayReturnType($saveArrayReturnType);
133. PHPExcel_Calculation:: getInstance()->writeDebugLog = $saveDebugLog;
134. }
136. /**
137. * Get delimiter
138. *
139. * @return string
140. */
141. public function getDelimiter() {
142. return $this->_delimiter;
143. }
145. /**
146. * Set delimiter
147. *
148. * @param string $pValue Delimiter, defaults to ,
149. * @return PHPExcel_Writer_CSV
150. */
151. public function setDelimiter($pValue = ',') {
152. $this->_delimiter = $pValue;
153. return $this;
154. }
156. /**
157. * Get enclosure
158. *
159. * @return string
160. */
161. public function getEnclosure() {
162. return $this->_enclosure;
163. }
165. /**
166. * Set enclosure
167. *
168. * @param string $pValue Enclosure, defaults to "
169. * @return PHPExcel_Writer_CSV
170. */
171. public function setEnclosure($pValue = '"') {
172. if ($pValue == '') {
173. $pValue = null;
174. }
175. $this->_enclosure = $pValue;
176. return $this;
177. }
179. /**
180. * Get line ending
181. *
182. * @return string
183. */
184. public function getLineEnding() {
185. return $this->_lineEnding;
186. }
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. $this->_lineEnding = $pValue;
196. return $this;
197. }
199. /**
200. * Get whether BOM should be used
201. *
202. * @return boolean
203. */
204. public function getUseBOM() {
205. return $this->_useBOM;
206. }
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. $this->_useBOM = $pValue;
216. return $this;
217. }
219. /**
220. * Get sheet index
221. *
222. * @return int
223. */
224. public function getSheetIndex() {
225. return $this->_sheetIndex;
226. }
228. /**
229. * Set sheet index
230. *
231. * @param int $pValue Sheet index
232. * @return PHPExcel_Writer_CSV
233. */
234. public function setSheetIndex($pValue = 0) {
235. $this->_sheetIndex = $pValue;
236. return $this;
237. }
239. /**
240. * Write line to CSV file
241. *
242. * @param mixed $pFileHandle PHP filehandle
243. * @param array $pValues Array containing values in a row
244. * @throws Exception
245. */
246. private function _writeLine($pFileHandle = null, $pValues = null) {
247. if ( is_array($pValues)) {
248. // No leading delimiter
249. $writeDelimiter = false;
251. // Build the line
252. $line = '';
254. foreach ($pValues as $element) {
255. // Escape enclosures
256. $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
258. // Add delimiter
259. if ($writeDelimiter) {
260. $line .= $this->_delimiter;
261. } else {
262. $writeDelimiter = true;
263. }
265. // Add enclosed string
266. $line .= $this->_enclosure . $element . $this->_enclosure;
267. }
269. // Add line ending
270. $line .= $this->_lineEnding;
272. // Write to file
273. fwrite($pFileHandle, $line);
274. } else {
275. throw new Exception("Invalid parameters passed.");
276. }
277. }
279. /**
280. * Get Pre-Calculate Formulas
281. *
282. * @return boolean
283. */
284. public function getPreCalculateFormulas() {
285. return $this->_preCalculateFormulas;
286. }
288. /**
289. * Set Pre-Calculate Formulas
290. *
291. * @param boolean $pValue Pre-Calculate Formulas?
292. * @return PHPExcel_Writer_CSV
293. */
294. public function setPreCalculateFormulas($pValue = true) {
295. $this->_preCalculateFormulas = $pValue;
296. return $this;
297. }
298. }

Documentation generated on Sun, 27 Feb 2011 16:29:13 -0800 by phpDocumentor 1.4.3