5
* Copyright (c) 2006 - 2009 PHPExcel
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.
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.
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
22
* @package PHPExcel_Writer
23
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
24
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25
* @version 1.7.0, 2009-08-10
29
/** PHPExcel root directory */
30
if (!defined('PHPEXCEL_ROOT')) {
34
define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../');
37
/** PHPExcel_IWriter */
38
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
41
require_once PHPEXCEL_ROOT . 'PHPExcel/Cell.php';
43
/** PHPExcel_RichText */
44
require_once PHPEXCEL_ROOT . 'PHPExcel/RichText.php';
46
/** PHPExcel_Shared_String */
47
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
54
* @package PHPExcel_Writer
55
* @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
57
class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
87
* Sheet index to write
94
* Pre-calculate formulas
98
private $_preCalculateFormulas = true;
101
* Whether to write a BOM (for UTF8).
105
private $_useBOM = false;
108
* Create a new PHPExcel_Writer_CSV
110
* @param PHPExcel $phpExcel PHPExcel object
112
public function __construct(PHPExcel $phpExcel) {
113
$this->_phpExcel = $phpExcel;
114
$this->_delimiter = ',';
115
$this->_enclosure = '"';
116
$this->_lineEnding = PHP_EOL;
117
$this->_sheetIndex = 0;
121
* Save PHPExcel to file
123
* @param string $pFileName
126
public function save($pFilename = null) {
128
$sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
130
$saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
131
PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
134
$fileHandle = fopen($pFilename, 'w');
135
if ($fileHandle === false) {
136
throw new Exception("Could not open file $pFilename for writing.");
139
if ($this->_useBOM) {
140
// Write the UTF-8 BOM code
141
fwrite($fileHandle, "\xEF\xBB\xBF");
144
// Convert sheet to array
145
$cellsArray = $sheet->toArray('', $this->_preCalculateFormulas);
147
// Write rows to file
148
foreach ($cellsArray as $row) {
149
$this->_writeLine($fileHandle, $row);
155
PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
163
public function getDelimiter() {
164
return $this->_delimiter;
170
* @param string $pValue Delimiter, defaults to ,
171
* @return PHPExcel_Writer_CSV
173
public function setDelimiter($pValue = ',') {
174
$this->_delimiter = $pValue;
183
public function getEnclosure() {
184
return $this->_enclosure;
190
* @param string $pValue Enclosure, defaults to "
191
* @return PHPExcel_Writer_CSV
193
public function setEnclosure($pValue = '"') {
197
$this->_enclosure = $pValue;
206
public function getLineEnding() {
207
return $this->_lineEnding;
213
* @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
214
* @return PHPExcel_Writer_CSV
216
public function setLineEnding($pValue = PHP_EOL) {
217
$this->_lineEnding = $pValue;
222
* Get whether BOM should be used
226
public function getUseBOM() {
227
return $this->_useBOM;
231
* Set whether BOM should be used
233
* @param boolean $pValue Use UTF-8 byte-order mark? Defaults to false
234
* @return PHPExcel_Writer_CSV
236
public function setUseBOM($pValue = false) {
237
$this->_useBOM = $pValue;
246
public function getSheetIndex() {
247
return $this->_sheetIndex;
253
* @param int $pValue Sheet index
254
* @return PHPExcel_Writer_CSV
256
public function setSheetIndex($pValue = 0) {
257
$this->_sheetIndex = $pValue;
262
* Write line to CSV file
264
* @param mixed $pFileHandle PHP filehandle
265
* @param array $pValues Array containing values in a row
268
private function _writeLine($pFileHandle = null, $pValues = null) {
269
if (!is_null($pFileHandle) && is_array($pValues)) {
270
// No leading delimiter
271
$writeDelimiter = false;
276
foreach ($pValues as $element) {
278
$element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
281
if ($writeDelimiter) {
282
$line .= $this->_delimiter;
284
$writeDelimiter = true;
287
// Add enclosed string
288
$line .= $this->_enclosure . $element . $this->_enclosure;
292
$line .= $this->_lineEnding;
295
fwrite($pFileHandle, $line);
297
throw new Exception("Invalid parameters passed.");
302
* Get Pre-Calculate Formulas
306
public function getPreCalculateFormulas() {
307
return $this->_preCalculateFormulas;
311
* Set Pre-Calculate Formulas
313
* @param boolean $pValue Pre-Calculate Formulas?
314
* @return PHPExcel_Writer_CSV
316
public function setPreCalculateFormulas($pValue = true) {
317
$this->_preCalculateFormulas = $pValue;