Source for file CSV.php
Documentation is available at CSV.php
* Copyright (c) 2006 - 2011 PHPExcel
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version 1.7.6, 2011-02-27
* @package PHPExcel_Writer
* @copyright Copyright (c) 2006 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
private $_delimiter = ',';
private $_enclosure = '"';
private $_lineEnding = PHP_EOL;
private $_sheetIndex = 0;
private $_preCalculateFormulas = true;
* Whether to write a BOM (for UTF8).
private $_useBOM = false;
* Create a new PHPExcel_Writer_CSV
* @param PHPExcel $phpExcel PHPExcel object
$this->_phpExcel = $phpExcel;
* @param string $pFileName
public function save($pFilename = null) {
$sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
$fileHandle = fopen($pFilename, 'wb+');
if ($fileHandle === false) {
throw new Exception("Could not open file $pFilename for writing.");
// Write the UTF-8 BOM code
fwrite($fileHandle, "\xEF\xBB\xBF");
// Convert sheet to array
$cellsArray = $sheet->toArray('', $this->_preCalculateFormulas);
foreach ($cellsArray as $row) {
$this->_writeLine($fileHandle, $row);
return $this->_delimiter;
* @param string $pValue Delimiter, defaults to ,
* @return PHPExcel_Writer_CSV
$this->_delimiter = $pValue;
return $this->_enclosure;
* @param string $pValue Enclosure, defaults to "
* @return PHPExcel_Writer_CSV
$this->_enclosure = $pValue;
return $this->_lineEnding;
* @param string $pValue Line ending, defaults to OS line ending (PHP_EOL)
* @return PHPExcel_Writer_CSV
$this->_lineEnding = $pValue;
* Get whether BOM should be used
* Set whether BOM should be used
* @param boolean $pValue Use UTF-8 byte-order mark? Defaults to false
* @return PHPExcel_Writer_CSV
$this->_useBOM = $pValue;
return $this->_sheetIndex;
* @param int $pValue Sheet index
* @return PHPExcel_Writer_CSV
$this->_sheetIndex = $pValue;
* @param mixed $pFileHandle PHP filehandle
* @param array $pValues Array containing values in a row
private function _writeLine($pFileHandle = null, $pValues = null) {
foreach ($pValues as $element) {
$element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
$line .= $this->_delimiter;
$line .= $this->_enclosure . $element . $this->_enclosure;
$line .= $this->_lineEnding;
throw new Exception("Invalid parameters passed.");
* Get Pre-Calculate Formulas
return $this->_preCalculateFormulas;
* Set Pre-Calculate Formulas
* @param boolean $pValue Pre-Calculate Formulas?
* @return PHPExcel_Writer_CSV
$this->_preCalculateFormulas = $pValue;
|