[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/Writer/ -> CSV.php (source)

   1  <?php
   2  
   3  /**
   4   * PHPExcel_Writer_CSV
   5   *
   6   * Copyright (c) 2006 - 2015 PHPExcel
   7   *
   8   * This library is free software; you can redistribute it and/or
   9   * modify it under the terms of the GNU Lesser General Public
  10   * License as published by the Free Software Foundation; either
  11   * version 2.1 of the License, or (at your option) any later version.
  12   *
  13   * This library is distributed in the hope that it will be useful,
  14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16   * Lesser General Public License for more details.
  17   *
  18   * You should have received a copy of the GNU Lesser General Public
  19   * License along with this library; if not, write to the Free Software
  20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  21   *
  22   * @category   PHPExcel
  23   * @package    PHPExcel_Writer_CSV
  24   * @copyright  Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  25   * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  26   * @version    ##VERSION##, ##DATE##
  27   */
  28  class PHPExcel_Writer_CSV extends PHPExcel_Writer_Abstract implements PHPExcel_Writer_IWriter
  29  {
  30      /**
  31       * PHPExcel object
  32       *
  33       * @var PHPExcel
  34       */
  35      private $phpExcel;
  36  
  37      /**
  38       * Delimiter
  39       *
  40       * @var string
  41       */
  42      private $delimiter    = ',';
  43  
  44      /**
  45       * Enclosure
  46       *
  47       * @var string
  48       */
  49      private $enclosure    = '"';
  50  
  51      /**
  52       * Line ending
  53       *
  54       * @var string
  55       */
  56      private $lineEnding    = PHP_EOL;
  57  
  58      /**
  59       * Sheet index to write
  60       *
  61       * @var int
  62       */
  63      private $sheetIndex    = 0;
  64  
  65      /**
  66       * Whether to write a BOM (for UTF8).
  67       *
  68       * @var boolean
  69       */
  70      private $useBOM = false;
  71  
  72      /**
  73       * Whether to write a fully Excel compatible CSV file.
  74       *
  75       * @var boolean
  76       */
  77      private $excelCompatibility = false;
  78  
  79      /**
  80       * Create a new PHPExcel_Writer_CSV
  81       *
  82       * @param    PHPExcel    $phpExcel    PHPExcel object
  83       */
  84      public function __construct(PHPExcel $phpExcel)
  85      {
  86          $this->phpExcel    = $phpExcel;
  87      }
  88  
  89      /**
  90       * Save PHPExcel to file
  91       *
  92       * @param    string        $pFilename
  93       * @throws    PHPExcel_Writer_Exception
  94       */
  95      public function save($pFilename = null)
  96      {
  97          // Fetch sheet
  98          $sheet = $this->phpExcel->getSheet($this->sheetIndex);
  99  
 100          $saveDebugLog = PHPExcel_Calculation::getInstance($this->phpExcel)->getDebugLog()->getWriteDebugLog();
 101          PHPExcel_Calculation::getInstance($this->phpExcel)->getDebugLog()->setWriteDebugLog(false);
 102          $saveArrayReturnType = PHPExcel_Calculation::getArrayReturnType();
 103          PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
 104  
 105          // Open file
 106          $fileHandle = fopen($pFilename, 'wb+');
 107          if ($fileHandle === false) {
 108              throw new PHPExcel_Writer_Exception("Could not open file $pFilename for writing.");
 109          }
 110  
 111          if ($this->excelCompatibility) {
 112              fwrite($fileHandle, "\xEF\xBB\xBF");    //    Enforce UTF-8 BOM Header
 113              $this->setEnclosure('"');                //    Set enclosure to "
 114              $this->setDelimiter(";");                //    Set delimiter to a semi-colon
 115              $this->setLineEnding("\r\n");
 116              fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
 117          } elseif ($this->useBOM) {
 118              // Write the UTF-8 BOM code if required
 119              fwrite($fileHandle, "\xEF\xBB\xBF");
 120          }
 121  
 122          //    Identify the range that we need to extract from the worksheet
 123          $maxCol = $sheet->getHighestDataColumn();
 124          $maxRow = $sheet->getHighestDataRow();
 125  
 126          // Write rows to file
 127          for ($row = 1; $row <= $maxRow; ++$row) {
 128              // Convert the row to an array...
 129              $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row, '', $this->preCalculateFormulas);
 130              // ... and write to the file
 131              $this->writeLine($fileHandle, $cellsArray[0]);
 132          }
 133  
 134          // Close file
 135          fclose($fileHandle);
 136  
 137          PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
 138          PHPExcel_Calculation::getInstance($this->phpExcel)->getDebugLog()->setWriteDebugLog($saveDebugLog);
 139      }
 140  
 141      /**
 142       * Get delimiter
 143       *
 144       * @return string
 145       */
 146      public function getDelimiter()
 147      {
 148          return $this->delimiter;
 149      }
 150  
 151      /**
 152       * Set delimiter
 153       *
 154       * @param    string    $pValue        Delimiter, defaults to ,
 155       * @return PHPExcel_Writer_CSV
 156       */
 157      public function setDelimiter($pValue = ',')
 158      {
 159          $this->delimiter = $pValue;
 160          return $this;
 161      }
 162  
 163      /**
 164       * Get enclosure
 165       *
 166       * @return string
 167       */
 168      public function getEnclosure()
 169      {
 170          return $this->enclosure;
 171      }
 172  
 173      /**
 174       * Set enclosure
 175       *
 176       * @param    string    $pValue        Enclosure, defaults to "
 177       * @return PHPExcel_Writer_CSV
 178       */
 179      public function setEnclosure($pValue = '"')
 180      {
 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      {
 195          return $this->lineEnding;
 196      }
 197  
 198      /**
 199       * Set line ending
 200       *
 201       * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
 202       * @return PHPExcel_Writer_CSV
 203       */
 204      public function setLineEnding($pValue = PHP_EOL)
 205      {
 206          $this->lineEnding = $pValue;
 207          return $this;
 208      }
 209  
 210      /**
 211       * Get whether BOM should be used
 212       *
 213       * @return boolean
 214       */
 215      public function getUseBOM()
 216      {
 217          return $this->useBOM;
 218      }
 219  
 220      /**
 221       * Set whether BOM should be used
 222       *
 223       * @param    boolean    $pValue        Use UTF-8 byte-order mark? Defaults to false
 224       * @return PHPExcel_Writer_CSV
 225       */
 226      public function setUseBOM($pValue = false)
 227      {
 228          $this->useBOM = $pValue;
 229          return $this;
 230      }
 231  
 232      /**
 233       * Get whether the file should be saved with full Excel Compatibility
 234       *
 235       * @return boolean
 236       */
 237      public function getExcelCompatibility()
 238      {
 239          return $this->excelCompatibility;
 240      }
 241  
 242      /**
 243       * Set whether the file should be saved with full Excel Compatibility
 244       *
 245       * @param    boolean    $pValue        Set the file to be written as a fully Excel compatible csv file
 246       *                                Note that this overrides other settings such as useBOM, enclosure and delimiter
 247       * @return PHPExcel_Writer_CSV
 248       */
 249      public function setExcelCompatibility($pValue = false)
 250      {
 251          $this->excelCompatibility = $pValue;
 252          return $this;
 253      }
 254  
 255      /**
 256       * Get sheet index
 257       *
 258       * @return int
 259       */
 260      public function getSheetIndex()
 261      {
 262          return $this->sheetIndex;
 263      }
 264  
 265      /**
 266       * Set sheet index
 267       *
 268       * @param    int        $pValue        Sheet index
 269       * @return PHPExcel_Writer_CSV
 270       */
 271      public function setSheetIndex($pValue = 0)
 272      {
 273          $this->sheetIndex = $pValue;
 274          return $this;
 275      }
 276  
 277      /**
 278       * Write line to CSV file
 279       *
 280       * @param    mixed    $pFileHandle    PHP filehandle
 281       * @param    array    $pValues        Array containing values in a row
 282       * @throws    PHPExcel_Writer_Exception
 283       */
 284      private function writeLine($pFileHandle = null, $pValues = null)
 285      {
 286          if (is_array($pValues)) {
 287              // No leading delimiter
 288              $writeDelimiter = false;
 289  
 290              // Build the line
 291              $line = '';
 292  
 293              foreach ($pValues as $element) {
 294                  // Escape enclosures
 295                  $element = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $element);
 296  
 297                  // Add delimiter
 298                  if ($writeDelimiter) {
 299                      $line .= $this->delimiter;
 300                  } else {
 301                      $writeDelimiter = true;
 302                  }
 303  
 304                  // Add enclosed string
 305                  $line .= $this->enclosure . $element . $this->enclosure;
 306              }
 307  
 308              // Add line ending
 309              $line .= $this->lineEnding;
 310  
 311              // Write to file
 312              fwrite($pFileHandle, $line);
 313          } else {
 314              throw new PHPExcel_Writer_Exception("Invalid data row passed to CSV writer.");
 315          }
 316      }
 317  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1