Subversion Repositories eFlore/Applications.cel

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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