[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/libraries/PHPExcel/PHPExcel/Writer/ -> CSV.php (source)

   1  <?php
   2  /**
   3   * PHPExcel
   4   *
   5   * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25   * @version    1.7.7, 2012-05-19
  26   */
  27  
  28  
  29  /**
  30   * PHPExcel_Writer_CSV
  31   *
  32   * @category   PHPExcel
  33   * @package    PHPExcel_Writer
  34   * @copyright  Copyright (c) 2006 - 2012 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;
  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       * Pre-calculate formulas
  74       *
  75       * @var boolean
  76       */
  77      private $_preCalculateFormulas = true;
  78  
  79      /**
  80       * Whether to write a BOM (for UTF8).
  81       *
  82       * @var boolean
  83       */
  84      private $_useBOM = 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    Exception
 100       */
 101  	public function save($pFilename = null) {
 102          // Fetch sheet
 103          $sheet = $this->_phpExcel->getSheet($this->_sheetIndex);
 104  
 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);
 109  
 110          // Open file
 111          $fileHandle = fopen($pFilename, 'wb+');
 112          if ($fileHandle === false) {
 113              throw new Exception("Could not open file $pFilename for writing.");
 114          }
 115  
 116          if ($this->_useBOM) {
 117              // Write the UTF-8 BOM code
 118              fwrite($fileHandle, "\xEF\xBB\xBF");
 119          }
 120  
 121          //    Identify the range that we need to extract from the worksheet
 122          $maxCol = $sheet->getHighestColumn();
 123          $maxRow = $sheet->getHighestRow();
 124  
 125          // Write rows to file
 126          for($row = 1; $row <= $maxRow; ++$row) {
 127              // Convert the row to an array...
 128              $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row,'', $this->_preCalculateFormulas);
 129              // ... and write to the file
 130              $this->_writeLine($fileHandle, $cellsArray[0]);
 131          }
 132  
 133          // Close file
 134          fclose($fileHandle);
 135  
 136          PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
 137          PHPExcel_Calculation::getInstance()->writeDebugLog = $saveDebugLog;
 138      }
 139  
 140      /**
 141       * Get delimiter
 142       *
 143       * @return string
 144       */
 145  	public function getDelimiter() {
 146          return $this->_delimiter;
 147      }
 148  
 149      /**
 150       * Set delimiter
 151       *
 152       * @param    string    $pValue        Delimiter, defaults to ,
 153       * @return PHPExcel_Writer_CSV
 154       */
 155  	public function setDelimiter($pValue = ',') {
 156          $this->_delimiter = $pValue;
 157          return $this;
 158      }
 159  
 160      /**
 161       * Get enclosure
 162       *
 163       * @return string
 164       */
 165  	public function getEnclosure() {
 166          return $this->_enclosure;
 167      }
 168  
 169      /**
 170       * Set enclosure
 171       *
 172       * @param    string    $pValue        Enclosure, defaults to "
 173       * @return PHPExcel_Writer_CSV
 174       */
 175  	public function setEnclosure($pValue = '"') {
 176          if ($pValue == '') {
 177              $pValue = null;
 178          }
 179          $this->_enclosure = $pValue;
 180          return $this;
 181      }
 182  
 183      /**
 184       * Get line ending
 185       *
 186       * @return string
 187       */
 188  	public function getLineEnding() {
 189          return $this->_lineEnding;
 190      }
 191  
 192      /**
 193       * Set line ending
 194       *
 195       * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
 196       * @return PHPExcel_Writer_CSV
 197       */
 198  	public function setLineEnding($pValue = PHP_EOL) {
 199          $this->_lineEnding = $pValue;
 200          return $this;
 201      }
 202  
 203      /**
 204       * Get whether BOM should be used
 205       *
 206       * @return boolean
 207       */
 208  	public function getUseBOM() {
 209          return $this->_useBOM;
 210      }
 211  
 212      /**
 213       * Set whether BOM should be used
 214       *
 215       * @param    boolean    $pValue        Use UTF-8 byte-order mark? Defaults to false
 216       * @return PHPExcel_Writer_CSV
 217       */
 218  	public function setUseBOM($pValue = false) {
 219          $this->_useBOM = $pValue;
 220          return $this;
 221      }
 222  
 223      /**
 224       * Get sheet index
 225       *
 226       * @return int
 227       */
 228  	public function getSheetIndex() {
 229          return $this->_sheetIndex;
 230      }
 231  
 232      /**
 233       * Set sheet index
 234       *
 235       * @param    int        $pValue        Sheet index
 236       * @return PHPExcel_Writer_CSV
 237       */
 238  	public function setSheetIndex($pValue = 0) {
 239          $this->_sheetIndex = $pValue;
 240          return $this;
 241      }
 242  
 243      /**
 244       * Write line to CSV file
 245       *
 246       * @param    mixed    $pFileHandle    PHP filehandle
 247       * @param    array    $pValues        Array containing values in a row
 248       * @throws    Exception
 249       */
 250  	private function _writeLine($pFileHandle = null, $pValues = null) {
 251          if (is_array($pValues)) {
 252              // No leading delimiter
 253              $writeDelimiter = false;
 254  
 255              // Build the line
 256              $line = '';
 257  
 258              foreach ($pValues as $element) {
 259                  // Escape enclosures
 260                  $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
 261  
 262                  // Add delimiter
 263                  if ($writeDelimiter) {
 264                      $line .= $this->_delimiter;
 265                  } else {
 266                      $writeDelimiter = true;
 267                  }
 268  
 269                  // Add enclosed string
 270                  $line .= $this->_enclosure . $element . $this->_enclosure;
 271              }
 272  
 273              // Add line ending
 274              $line .= $this->_lineEnding;
 275  
 276              // Write to file
 277              fwrite($pFileHandle, $line);
 278          } else {
 279              throw new Exception("Invalid parameters passed.");
 280          }
 281      }
 282  
 283      /**
 284       * Get Pre-Calculate Formulas
 285       *
 286       * @return boolean
 287       */
 288  	public function getPreCalculateFormulas() {
 289          return $this->_preCalculateFormulas;
 290      }
 291  
 292      /**
 293       * Set Pre-Calculate Formulas
 294       *
 295       * @param boolean $pValue    Pre-Calculate Formulas?
 296       * @return PHPExcel_Writer_CSV
 297       */
 298  	public function setPreCalculateFormulas($pValue = true) {
 299          $this->_preCalculateFormulas = $pValue;
 300          return $this;
 301      }
 302  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1